Search code examples
cif-statementlogical-andrelational-operators

Why is my C program not showing any output in the terminal when I try to find the youngest person's age among three given ages?


the terminal is showing no output (blank)

// if the ages of 3 people are put telling which one is youngest//

#include<stdio.h>

int main()
{
    int a1,a2,a3;

    printf("enter the age of ram,shyam and ajay");
    scanf("%d,%d,%d",&a1,&a2,&a3);

    if(a1>a2>a3)
        printf("%d,%d,%d",a1,a2,a3);
    if(a3>a2>a1)
        printf("%d,%d,%d",a3,a2,a1);
    if(a2>a1>a3)
        printf("%d,%d,%d",a2,a1,a3);
    if(a2>a3>a1)
        printf("%d,%d,%d",a2,a3,a1);
    if(a1>a3>a2)
        printf("%d,%d,%d",a1,a3,a2);
    if(a3>a1>a2)
        printf("%d,%d,%d",a3,a1,a2);

    return 0;
}

i am not sure if you should write 3 expressions in an if statement like 1>2>3 please correct me if i am wrong


Solution

  • The line

    if(a1>a2>a3)
    

    will not do what you want. According to the rules of operator precedence and associativity, that expression is equivalent to the following:

    if ( (a1>a2) > a3 )
    

    The sub-expression (a1>a2) will evaluate to either 0 (false) or 1 (true). Therefore, the entire expression is equivalent to either

    if ( 0 > a3 )
    

    or:

    if ( 1 > a3 )
    

    What you want is instead the following:

    if ( ( a1 > a2 ) && ( a2 > a3 ) )
    

    Because the && operator has lower precedence than the > operator according to the rules mentioned above, you can write this without the parentheses:

    if ( a1 > a2 && a2 > a3 )
    

    Another problem in your solution is that if all 3 input values are identical, then none of the if statements will be true, and your program will print nothing.

    I suggest that you rewrite your logic like this:

    #include <stdio.h>
    
    int main( void )
    {
        int a1, a2, a3;
    
        printf( "Enter the age of ram,shyam and ajay: " );
        scanf( "%d,%d,%d", &a1, &a2, &a3 );
    
        if ( a1 > a2 )
        {
            if ( a2 > a3 )
            {
                printf( "%d,%d,%d\n", a1, a2, a3 );
            }
            else
            {
                if ( a1 > a3 )
                {
                    printf( "%d,%d,%d\n", a1, a3, a2 );
                }
                else
                {
                    printf( "%d,%d,%d\n", a3, a1, a2 );
                }
            }
        }
        else
        {
            if ( a1 > a3 )
            {
                printf( "%d,%d,%d\n", a2, a1, a3 );
            }
            else
            {
                if ( a2 > a3 )
                {
                    printf( "%d,%d,%d\n", a2, a3, a1 );
                }
                else
                {
                    printf( "%d,%d,%d\n", a3, a2, a1 );
                }
            }
        }
    
        return 0;
    }
    

    Because every if statement now has a corresponding else statement, all cases are handled, including the case of all 3 numbers being identical.

    This solution is also more efficient, because the maximum number of comparisons required is now only 3 instead of 12.

    Alternatively, if you put all numbers into an array, you can sort the array and then print it. This will make things a lot easier when you have more than 3 numbers:

    #include <stdio.h>
    #include <stdlib.h>
    
    int my_compare( const void *left, const void *right )
    {
        int a = *(int*)left;
        int b = *(int*)right;
    
        if ( a < b )
            return 1;
        if ( a > b )
            return -1;
    
        return 0;
    }
    
    int main( void )
    {
        //define array with many numbers
        int arr[] = { 14, 17, 19, 18, 17, 12, 15, 19 };
        const int arr_size = sizeof arr / sizeof *arr;
    
        //sort the array
        qsort( arr, arr_size, sizeof *arr, my_compare );
    
        //print the content of the sorted array
        for ( int i = 0; ; )
        {
            printf( "%d", arr[i] );
            if ( ++i == arr_size )
                break;
            printf( ", " );
        }
    
        return 0;
    }
    

    This program has the following output:

    19, 19, 18, 17, 17, 15, 14, 12