Write a C program to find the maximum number between three numbers
Description: Take three numbers as input and print the largest one.
Hint:
- Compare the numbers using the
>=and&&operators.
C program to find the largest of three numbers
#include <stdio.h>
int main(void) {
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
max = a;
} else if (b >= a && b >= c) {
max = b;
} else {
max = c;
}
printf("Largest = %d\n", max);
return 0;
}
Output:
Output of the above program is
Enter three numbers: 12 45 30
Largest = 45
How it works
- We check if
ais greater than or equal to both others. If not, we checkb, and otherwisecmust be the largest. - Using
>=also works when two numbers are equal.
Learn more about the similar topics:
| Tutorials |
|---|
| C If Else Statement |
| C Else If Ladder and Nested If |
| C Switch Statement |
| C While Loop |
| C Do While Loop |
| C For Loop |
| C Nested Loops |