C Program to Find the Largest Number Among Three Numbers
This program uses only if statement to find the largest number.
Example #1
This program uses nested if…else statement to find the largest number.
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if( n1>=n2 && n1>=n3 )
printf("%.2f is the largest number.", n1);
if( n2>=n1 && n2>=n3 )
printf("%.2f is the largest number.", n2);
if( n3>=n1 && n3>=n2 )
printf("%.2f is the largest number.", n3);
return 0;
}
This program uses if…else statement to find the largest number.
Example #2
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1>=n2)
{
if(n1>=n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}
else
{
if(n2>=n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.",n3);
}
return 0;
}
Example #3
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if( n1>=n2 && n1>=n3)
printf("%.2lf is the largest number.", n1);
else if (n2>=n1 && n2>=n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
return 0;
}
Though, the largest number among three numbers is found using multiple ways, the output of all these program will be same.
Enter three numbers: -4.5 3.9 5.6 5.60 is the largest number.
program in C that finds the largest number among three numbers:
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3) {
printf("%d is the largest number.\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("%d is the largest number.\n", num2);
} else {
printf("%d is the largest number.\n", num3);
}
return 0;
}
In this program, we declare three integer variables “num1”, “num2”, and “num3”, and prompt the user to enter three integer values using the “scanf” function.
We then use a series of “if” and “else if” statements to compare the values of “num1”, “num2”, and “num3” and determine which one is the largest. The condition in each statement checks whether the corresponding number is greater than or equal to the other two numbers. If it is, we print out a message indicating that it is the largest number. If none of the conditions are true, it means that all three numbers are equal, and we can print out a message indicating that as well.
Finally, we return 0 from the main function to indicate that the program has executed successfully. The output of this program would be:
Enter three numbers: 5 10 2
10 is the largest number.
If the user had entered three equal numbers, such as 4, 4, and 4, the output would be:
Enter three numbers: 4 4 4
All numbers are equal.