Find Largest Using If-Else
#include <stdio.h>
int main() {
float a, b, c, largest;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
largest = a;
if (b > largest) {
largest = b;
}
if (c > largest) {
largest = c;
}
printf("Largest number = %.2f\n", largest);
return 0;
}
Enter three numbers: 2 9 6
Largest number = 9.00
By initially assuming the first number is largest and comparing it with the other two, this approach updates the largest variable whenever a greater value is found.
Find Largest Using Ternary Operator
#include <stdio.h>
int main() {
float a, b, c, largest;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
largest = (a > b && a > c) ? a : ((b > c) ? b : c);
printf("Largest number = %.2f\n", largest);
return 0;
}
Enter three numbers: 5 1 9
Largest number = 9.00
The ternary operator provides a concise way to compare multiple values. By nesting conditional expressions, you can evaluate which of the three numbers is the largest in a single statement.
