C Language: Program to Find the Largest Number Among Three Numbers

Example:1
The below program uses “If statement” to find the Largest Number Among Three Numbers:

#include <stdio.h>
int main()
{
int n1, n2, n3;

printf(“Enter three numbers: “);
scanf(“%d %d %d”, &n1, &n2, &n3);

if( n1>=n2 && n1>=n3 )
printf(“%d is the largest number.”, n1);

if( n2>=n1 && n2>=n3 )
printf(“%d is the largest number.”, n2);

if( n3>=n1 && n3>=n2 )
printf(“%d is the largest number.”, n3);

return 0;
}




Example:2
The below program uses “if else statement” to find the Largest Number Among Three Numbers:

#include <stdio.h>
int main()
{
int n1, n2, n3;

printf(“Enter three numbers: “);
scanf(“%d %d %d”, &n1, &n2, &n3);

if (n1>=n2)
{
if(n1>=n3)
printf(“%d is the largest number.”, n1);
else
printf(“%d is the largest number.”, n3);
}
else
{
if(n2>=n3)
printf(“%d is the largest number.”, n2);
else
printf(“%d is the largest number.”,n3);
}
return 0;
}




Example:3
The below program uses nested if else statement to find the Largest Number Among Three Numbers:

#include <stdio.h>
int main()
{
double n1, n2, n3;

printf(“Enter three numbers: “);
scanf(“%d %d %d”, &n1, &n2, &n3);

if( n1>=n2 && n1>=n3)
printf(“%d is the largest number.”, n1);

else if (n2>=n1 && n2>=n3)
printf(“%d is the largest number.”, n2);

else
printf(“%d is the largest number.”, n3);

return 0;
}




Out put:
Enter three numbers:
300
600
900
900 is the largest number

Translate »