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
a
is greater than or equal to bothb
andc
,a
is the largest. - If
b
is greater than or equal to botha
andc
,b
is the largest. - If neither
a
norb
is the largest, thenc
must be the largest.
The largest number is then printed to the console.
Comments
Post a Comment