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
The line
will not do what you want. According to the rules of operator precedence and associativity, that expression is equivalent to the following:
The sub-expression
(a1>a2)will evaluate to either0(false) or1(true). Therefore, the entire expression is equivalent to eitheror:
What you want is instead the following:
Because the
&&operator has lower precedence than the>operator according to the rules mentioned above, you can write this without the parentheses:Another problem in your solution is that if all 3 input values are identical, then none of the
ifstatements will be true, and your program will print nothing.I suggest that you rewrite your logic like this:
Because every
ifstatement now has a correspondingelsestatement, 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:
This program has the following output: