Greater of two numbers
Program to find out which of the two numbers entered by the user is greater.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
printf("Enter the first number\t: ");
scanf("%d",&num1);
printf("Enter the second number\t: ");
scanf("%d",&num2);
if(num1>num2)
{
printf("\n%d is greater",num1);
}
else if(num2>num1)
{
printf("\n%d is greater",num2);
}
else
{
printf("\nBoth the numbers are equal");
}
getch();
}
Output
********** Run1 **********
Enter the first number : 5
Enter the second number : 4
5 is greater
********** Run2 **********
Enter the first number : 4
Enter the second number : 8
8 is greater
Explanation
In above problem, three cases arises
first number is greater than the second number
second number is greater than the first number
both the numbers are equal
so, we have to check on all the above three cases.
Two variables, 'num1' and 'num2' are used to store the values of both the numbers entered by user. if-else construct is used to accomplish the above task.
The above statement checks whether the first number is greater than the second number or not. If num1 is greater that num2, the above condition evaluates to true and if block is executed.
This statement prints that first number (value) is greater and the control comes out of the entire if-else construct.
If above condition results in false, then, the control goes to the following statement
This statement checks whether the second number is greater than the first number or not. If num2 is greater that num1, the above condition evaluates to true and else if block is executed.
This statement prints that second number (value) is greater and the control comes out of the entire if-else construct.
If both the above cases are false, then it is obvious that both the numbers are equal. So, there is no need to check any condition. So, else block can be used, where no condition is checked and the block is executed.
This statement prints that both the numbers are equal.
Two variables, 'num1' and 'num2' are used to store the values of both the numbers entered by user. if-else construct is used to accomplish the above task.
if(num1>num2)
The above statement checks whether the first number is greater than the second number or not. If num1 is greater that num2, the above condition evaluates to true and if block is executed.
printf("\n%d is greater",num1);
This statement prints that first number (value) is greater and the control comes out of the entire if-else construct.
If above condition results in false, then, the control goes to the following statement
else if(num2>num1)
This statement checks whether the second number is greater than the first number or not. If num2 is greater that num1, the above condition evaluates to true and else if block is executed.
printf("\n%d is greater",num2);
This statement prints that second number (value) is greater and the control comes out of the entire if-else construct.
If both the above cases are false, then it is obvious that both the numbers are equal. So, there is no need to check any condition. So, else block can be used, where no condition is checked and the block is executed.
else
{
printf("\nBoth the numbers are equal");
}
This statement prints that both the numbers are equal.