Find Largest of Three Numbers in C
Find the Largest of Three Numbers in C
This C program takes three numbers as input and determines the largest among them. Here's the code:
#include <stdio.h>
int main() {
int a, b, c;
// Input the numbers
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
printf("Enter the third number: ");
scanf("%d", &c);
// Determine the largest number
if (a >= b && a >= c) {
printf("The largest number is %d\n", a);
} else if (b >= a && b >= c) {
printf("The largest number is %d\n", b);
} else {
printf("The largest number is %d\n", c);
}
return 0;
}
Explanation
The program starts by including the <stdio.h> header file to use the input and output functions.
In the main function, we declare three integer variables: a, b, and c.
We then prompt the user to enter three numbers and read them using scanf.
The program uses a series of if-else statements to compare the numbers:
- If
ais greater than or equal to bothbandc,ais the largest. - If
bis greater than or equal to bothaandc,bis the largest. - If neither
anorbis the largest, thencmust be the largest.
The largest number is then printed to the console.
Comments
Post a Comment