Like Tree1Likes
  • 1 Post By ledow

Always give me wrong answer

This is a discussion on Always give me wrong answer within the C Programming forums, part of the General Programming Boards category; Hi , guys i have problem to solve this i write the code and didn't give any error but give ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    8

    Always give me wrong answer

    Hi , guys i have problem to solve this i write the code and didn't give any error but give me wrong answer when i run the program

    This is the problem:
    Write a program that prompts user to enter an integer number and determines whether it is
    even or odd , whether it is divisible by 5 and 7 , whether it is divisible by 5 or 7 , whether it is
    divisible by 5 or 7 , but not both .


    This is the code i write
    Code:
    #include<stdio.h>
    void main()
    {
        int num = 0 , remainder = 0;
        int num1 = 0 , num2 = 0;
        printf("please Enter an integer number:");
        scanf("%d" , &num);
        {
    remainder = num %2;
        if (remainder==0)
        printf("%d is Even number \n " ,num);
        else
    printf("%d is Odd number \n " ,num);
    }
    
        {
    num1 = num %5;
    num2 = num %7;
    {
    if((num1==0) && (num2==0))
    printf("%d true \n");
    else
    printf("%d false \n");
    }
    {
    if((num=1==0) || (num2==0))
    printf("%d true \n");
    else
    printf("%d false \n");
    }
    {
    if(((num1==0) || (num2==0)) && ((num1==0) && (num2==0)))
    printf("%d true \n");
    else
    printf("%d false \n");
    }
        }
    }

  2. #2
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Please reformat your code before we proceed. People here, and that includes me, get very annoyed when we want to help someone and they're making it hard for us to. It's akin to saving a panicking drowning person.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Seeing as you've totally ignored all the compiler warnings also, I won't bother to mention them in detail. Hint: EVERY Printf being wrong? Really?


    Also, you have glaring errors, like:

    Code:
    if((num=1==0) || (num2==0))
    (stray ='s in the middle of num and 1)

    and the last if? WTH is that trying to do?

    Code:
    if(((num1==0) || (num2==0)) && ((num1==0) && (num2==0)))
    MK27 likes this.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    No offence mate, but not everything you barf in an editor is code. I mean how can you post here and ask for questions without having compiled it at least once. There are so many wrong things with that code that even a child will be able to fix at least one or two very very very OBVIOUS ones.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by amroto View Post
    Write a program that prompts user to enter an integer number and determines whether it is
    even or odd , whether it is divisible by 5 and 7 , whether it is divisible by 5 or 7 , whether it is
    divisible by 5 or 7 , but not both .
    And I bet what you did is write all of the code to do that, figuring once it was finished you could try it out then go back and fix whatever appeared to be wrong.

    That is a common enough beginner mistake, but it is not how you program. You program by breaking a task down into smaller parts and coding one section at a time in such a way that you can compile and test it, because you actually need to compile and test it, making sure it works the way you want it to before you move on. This may mean you have to temporarily shim something in to do the test (eg, for the first bit, getting an input, you will want a temporary printf to check if the input is correct). One reason it is stupid and a waste of time to do anything else is because if you are wrong about how the first thing works and everything after that is based on an incorrect assumption, everything else may be garbage once you fix the first part. Another is that you are likely to repeat the same basic mistake over and over again, as you did with the printfs.

    It looks to me like you do have the first two bits of this correct enough, ie, getting an input and determining if it is odd or even. Everything after that is wrong, so scrap it, and try to code just the next bit ("whether it is divisible by 5 and 7"). When that works, you can move on.

    And do take seriously msh and ledow's comments about:
    a) correctly formatting (indenting) code.
    b) turning on and paying attention to compiler warnings.
    If you can't do that, most people here will not entertain you for long.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    8
    Quote Originally Posted by msh View Post
    Please reformat your code before we proceed. People here, and that includes me, get very annoyed when we want to help someone and they're making it hard for us to. It's akin to saving a panicking drowning person.
    sorry my friend but really i dont understadand what you mean because my weak english language but if mean what is the language i use ,so i use C-language

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    8
    Quote Originally Posted by ledow View Post
    Seeing as you've totally ignored all the compiler warnings also, I won't bother to mention them in detail. Hint: EVERY Printf being wrong? Really?


    Also, you have glaring errors, like:

    Code:
    if((num=1==0) || (num2==0))
    (stray ='s in the middle of num and 1)

    and the last if? WTH is that trying to do?

    Code:
    if(((num1==0) || (num2==0)) && ((num1==0) && (num2==0)))
    Thank you i didn't notice that , and yes now the program run well and give me right answer

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by amroto View Post
    sorry my friend but really i dont understadand what you mean because my weak english language but if mean what is the language i use ,so i use C-language
    S/he means this:

    Indent style - Wikipedia, the free encyclopedia

    This is very important, particularly if you want other people to read and understand your code.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    8
    thanks for all who cares about my topic and try to learn me something new

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pls give me the answer
    By meazeem in forum C Programming
    Replies: 2
    Last Post: 03-10-2011, 04:29 AM
  2. Replies: 3
    Last Post: 09-06-2009, 02:48 PM
  3. Wrong answer?
    By eViLrAcEr in forum C++ Programming
    Replies: 5
    Last Post: 07-16-2009, 07:04 AM
  4. Question 26 Answer seems wrong on site quiz
    By Kleid-0 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2005, 04:03 PM
  5. wrong answer?
    By cman in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 02:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21