C Program To Find Largest Of 5 Numbers Using Ternary Operator
In this tutorial, we are going to see how to write a C program to find the largest of 5 numbers using ternary operator. In the following example, we ask the user to enter 5 numbers using scanf function, and then we consider the first input as the maximum. Then we compare it with other inputs.
C Program To Find Largest Of 5 Numbers Using Ternary Operator
#include <stdio.h>
int main()
{
int a,b,c,d,e;
printf("Enter 1st number: ");
scanf("%d",&a);
printf("Enter 2nd number: ");
scanf("%d",&b);
printf("Enter 3th number: ");
scanf("%d",&c);
printf("Enter 4th number: ");
scanf("%d",&d);
printf("Enter 5th number: ");
scanf("%d",&e);
int max = a;
max = (b > max)? b: max;
max = (c > max)? c: max;
max = (d > max)? d: max;
max = (e > max)? e: max;
printf("\nMax is %d", max);
return 0;
}
Output:
Enter 1st number: 5 Enter 2nd number: 12 Enter 3th number: 4 Enter 4th number: 6 Enter 5th number: 7 Max is 12





Using the 5 var list for the ternary operator doesn’t seem to work properly if the fifth var has the greatest value?