Enter the values of three numbers and find out the largest number using nested if ( means if-else contain multiple if-else statements ):
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
scanf("%d%d%d" ,&a,&b,&c);
if(a>b) // if statement contain if-else statement
{
if(a>c)
printf(" a is larger ");
else // Means (c>a)
printf(" c is larger ");
}
else // Means (b>a)
{
if(b>c)
printf(" b is larger ");
else // Means (c>b)
printf(" c is larger ");
}
getch();
}
// End of main()
Output:
Enter the values of a,b and c:
2
4
6
c is larger
// (a>b) condition is False so comes out of the loop and control transfer to else part i.e (b>a).
(b>a) condition is True so enter in the else loop but (b>c) condition is False so control transfer to else part i.e (c>b). (c>b) condition is True so execute the statement c is larger.
NOTE: Follow the same procedure for the different values and check the output.
0 Response to "C Program: Find out the Largest Number by using Nested if Statement:"
Post a Comment