Task: Read three integers and determine the largest using nestedif
statements. Also report if two numbers tie for largest or if all three are equal.
/*
Program: Largest of Three (nested if-else) — U1
What it does:
- Reads integers a, b, c.
- Uses nested if-else to print which value is largest.
- Also handles: a==b==c (all equal) and "two numbers tie for largest".
*/
#include <stdio.h>
int main(void) {
int a, b, c;
printf("Enter three integers (a b c): ");
if (scanf("%d %d %d", &a, &b, &c) != 3) {
printf("Invalid input. Please enter three integers.\n");
return 0;
}
/* All equal? Handle upfront. */
if (a == b && b == c) {
printf("All three numbers are equal: %d\n", a);
} else {
/* Nested comparisons */
if (a >= b) {
if (a > c) {
/* a is strictly largest; if a==b (possible because a>=b), b<a since a>c and c compares against a */
if (a == b) {
printf("Two numbers tie for largest: a and b = %d\n", a);
} else {
printf("Largest = %d (a)\n", a);
}
} else if (a == c) {
printf("Two numbers tie for largest: a and c = %d\n", a);
} else {
printf("Largest = %d (c)\n", c);
}
} else { /* b > a */
if (b > c) {
printf("Largest = %d (b)\n", b);
} else if (b == c) {
printf("Two numbers tie for largest: b and c = %d\n", b);
} else {
printf("Largest = %d (c)\n", c);
}
}
}
/* Examples:
a=10, b=7, c=9 -> Largest = 10 (a)
a=8, b=15, c=15 -> Two numbers tie for largest: b and c = 15
a=5, b=5, c=5 -> All three numbers are equal: 5
*/
return 0;
}