Greatest of three numbers using nested if-else

C program to find the greatest of three numbers using Nested if-else.

Program

#include<stdio.h>
#include<conio.h>
void main()
{
	int num1, num2, num3;

    printf("Enter the first number\t:");
    scanf("%d",&num1);
    printf("Enter the second number\t:");
    scanf("%d",&num2);
    printf("Enter the third number\t:");
    scanf("%d",&num3);

    if (num1>num2)
    {
        if (num1>num3)
        {
            printf("\n%d is greatest",num1);
		}
		else
		{
	        printf("\n%d is greatest",num3);
		}
    }
    else
    {
        if (num2>num3)
        {
            printf("\n%d is greatest",num2);
		}
		else
		{
	        printf("\n%d is greatest",num3);
		}
    }

    getch();
}

Output

********** Run1 ********** 

Enter the first number  :12
Enter the second number :23
Enter the third number  :21

23 is greatest


********** Run2 ********** 

Enter the first number  :23
Enter the second number :34
Enter the third number  :45

45 is greatest


********** Run3 ********** 

Enter the first number  :34
Enter the second number :32
Enter the third number  :21

34 is greatest

Explanation

Three numbers are taken as input from the user in the variables named 'num1', 'num2' and 'num3' respective;y.

First of all, check if 'num1' is greater than 'num2'
if condition is true, then check if 'num1' is greater than 'num3'
if condition is true, then 'num1' is greater than both 'num2' and 'num3'. So, 'num1' is greatest.
if condition is false, then it means num1>num2 and num1<num3. As 'num2' is lesser than 'num1', so it becomes obvious that it is also lesser than 'num3'. So, 'num3' became the greatest.
if condition is false, then it means 'num2' is greater than 'num1'. Now, check whether 'num2' is greater than 'num3' also.
if condition is true, then 'num2' is greater than both 'num1' and 'num3'. So 'num2' is greatest.
if condition is false, then it means num2>num1 and num2<num3. From these teo conditions it came out that 'num3' is greatest.