C Program to Find Largest Among Four Numbers
The following example shows, how to find largest among four numbers.
- Takes four integer inputs from the user.
- Uses nested if statements to compare all four numbers.
- Determines the largest number through step-by-step comparisons.
- Displays the largest number as the output.
Example : pgm.c
#include <stdio.h> void main() { int a, b, c, d, largest; // Input four numbers printf("Enter four numbers: "); scanf("%d %d %d %d", &a, &b, &c, &d); // Nested if logic to find the largest if (a > b) { if (a > c) { if (a > d) largest = a; else largest = d; } else { if (c > d) largest = c; else largest = d; } } else { if (b > c) { if (b > d) largest = b; else largest = d; } else { if (c > d) largest = c; else largest = d; } } printf("The largest number is: %d\n", largest); }
Output:
Enter four numbers: 11 49 15 26
The largest number is: 49
The largest number is: 49