Thread: problem in if statement

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    problem in if statement

    I'm a beginner on C programming so some of you might find the code I'm writing is silly. I have been working for a simple program that ask user for 3 numbers, and provide results such as sum, average, product, smallest and largest number. I come up with this:

    Code:
    #include<stdio.h>#include<stdlib.h>
    
    
    int main()
    {
        int a;
        int b;
        int c;
        
        printf("Enter number 1: ");
        scanf("%d", &a);
        
        printf("Enter number 2: ");
        scanf("%d", &b);
        
        printf("Enter number 3: ");
        scanf("%d", &c);
        
        printf("\nSum: %d \n", a+b+c);
        
        printf("Average is: %d \n", (a+b+c)/3);
        
        printf("Product is: %d \n", a*b*c);
        
        if(a<b && a<c)
        {
            printf("Smallest number is: %d \n", a);
        }
        
        else if(b<a && b<c)
        {
            printf("Smallest number is: %d \n", b);
        }
        
        else
        {
            printf("Smallest number is: %d \n", c);
        }
        
        if(a>b && a>c)
        {
            printf("Largest number is: %d \n", a);
        }
        
        else if(b>a && b>c)
        {
            printf("Largest number is: %d \n", b);
        }
        
        else
        {
            printf("Largest number is: %d \n", c);
        }
        
        system("pause");
        return 0;
    }

    The problem is whenever I put the same number in number 2 and 3, it automatically choose number 3 as the smallest and the largest number. What am I missing here? Thanks!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Sit down with a pencil and paper and follow your variables through the program... step by step... you'll find it.

    Troubleshooting is such a basic programming skill, you should be glad of the opportunity to work on it.

  3. #3
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Check your logic for this part:

    Code:
    else if(b<a && b<c)    {
            printf("Smallest number is: %d \n", b);
        }
         
        else
        {
            printf("Smallest number is: %d \n", c);
        }
    and the same this for the largest number. It's doing exactly what you are telling it to do.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You'll also find the average is wrong. Try it for 3,3,4. The output will be 3 rather than 3.33333333 because you are doing integer division.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Quote Originally Posted by rmatze View Post
    Check your logic for this part:

    Code:
    else if(b<a && b<c)    {
            printf("Smallest number is: %d \n", b);
        }
         
        else
        {
            printf("Smallest number is: %d \n", c);
        }
    and the same this for the largest number. It's doing exactly what you are telling it to do.
    Okay I found my mistake. When I put a=2, b=2, c=3 it will not print a or b as the smallest because a<b and a<c is not true. Do I need to learn other operator to solve this?

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    Sit down with a pencil and paper and follow your variables through the program... step by step... you'll find it.

    Troubleshooting is such a basic programming skill, you should be glad of the opportunity to work on it.
    I'm doing it now

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Quote Originally Posted by TheBigH View Post
    You'll also find the average is wrong. Try it for 3,3,4. The output will be 3 rather than 3.33333333 because you are doing integer division.
    my bad, my knowledge of programming language is still very limited. Let me know if you have other solution

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would try <= instead of < and see if it works.

    Note: My logic would be very different. I would have a smallest and largest variable that I set to the smaller/larger value.

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with ; at end of if statement
    By Bujo0 in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2011, 08:13 AM
  2. ; problem near if statement
    By orhanli1 in forum C Programming
    Replies: 3
    Last Post: 01-17-2011, 07:42 AM
  3. Problem with if statement
    By Sharifhs in forum C Programming
    Replies: 3
    Last Post: 08-11-2010, 05:02 PM
  4. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  5. problem if statement
    By irncty99 in forum C++ Programming
    Replies: 2
    Last Post: 07-14-2003, 09:33 AM