Greatest of three numbers using else-if ladder
C program to find the greatest of three numbers using Else-if ladder.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num1, num2, num3;
printf("Enter first number\t:");
scanf("%d",&num1);
printf("Enter second number\t:");
scanf("%d",&num2);
printf("Enter third number\t:");
scanf("%d",&num3);
if((num1>=num2) && (num1>=num3))
{
printf("\nFirst number %d is greatest",num1);
}
else if((num2>=num1) && (num2>=num3))
{
printf("\nSecond number %d is greatest",num2);
}
else if((num3>=num1) && (num3>=num2))
{
printf("\nThird number %d is greatest",num3);
}
getch();
}
Output
********** Run1 **********
Enter first number :5
Enter second number :4
Enter third number :3
First number 5 is greatest
********** Run2 **********
Enter first number :3
Enter second number :10
Enter third number :18
Third number 18 is greatest
********** Run3 **********
Enter first number :3
Enter second number :10
Enter third number :8
Second number 10 is greatest
Explanation
Three numbers are taken as input from the user in the variables named 'num1', 'num2' and 'num3'.
First of all, first number is checked for greatest of the three. If 'num1' is greater than 'num2' and it is also greater than 'num3' then, first number 'num1' is greatest. This is checked using the condition:
If above condition results in false, then second number is checked for greatest of the three. If 'num2' is greater than 'num1' and it is also greater than 'num3' then, second number 'num2' is greatest. This is checked using the condition:
If above condition results in false, then third number is checked for greatest of the three. If 'num3' is greater than 'num1' and it is also greater than 'num2' then, third number 'num3' is greatest. This is checked using the condition:
First of all, first number is checked for greatest of the three. If 'num1' is greater than 'num2' and it is also greater than 'num3' then, first number 'num1' is greatest. This is checked using the condition:
if(num1>=num2 && num1>=num3)
If above condition results in false, then second number is checked for greatest of the three. If 'num2' is greater than 'num1' and it is also greater than 'num3' then, second number 'num2' is greatest. This is checked using the condition:
else if(num2>=num1 && num2>=num3)
If above condition results in false, then third number is checked for greatest of the three. If 'num3' is greater than 'num1' and it is also greater than 'num2' then, third number 'num3' is greatest. This is checked using the condition:
else if(num3>=num1 && num3>=num2)