Write a C program to find the maximum from given 3 numbers using nested if

Find maximum among 3 numbers is pretty easy if you know the concept of nested-if statements.

As mentioned in the problem statement, we have to find the solution by using nested-if. In simple words, nested-if means, if statement within another if statement.

## Tutorial 📺

Watch tutorial and learn how to solve this problem. 😉

## Steps 🪜

### To find maximum among 3 numbers

1. Accept 3 numbers from the user and store them in variables n1, n2 and n3.
2. First, check if (n1 > n2) is true or not. If it returns true then check if (n1 > n3) return true then print a message that n1 is maximum.
3. Next, same way compare the value of n2 with n1 and n3. Like if (n2 > n1) returns true only then check if (n2 > n3). If the nested if-condition also returns true then print a message that n2 is maximum.
4. Finally, compare n3 with n1 and n2. If both outer and inner conditions return true then display the message n3 is maximum.

## Explanation 📖

In order to find maximum among 3 numbers, we need to compare each number if it is maximum than other two numbers or not.

Let’s assume that n1 is maximum which is possible only if n1 is greater than n2 as well as n3. So, in that case, we can use two if-statement, one inside another i.e. nested-if statement.

The first if-statement states that if n1 is greater than n2 then inside this if-block, there is another if-statement which tests if n1 is greater than n3 and within this nested-if block print the message that value of n1 is maximum.

Now, let’s assume that n2 is maximum which is possible only if n2 is greater than n1 as well as n3. Again, we will use nested-if statement to implement both if conditions.

The first if-statement states that if n2 is greater than n1 then inside this if-block, there is another if-statement which tests if n2 is greater than n3 and within this nested-if block print the message that value of n2 is maximum.

Now, let’s assume that n3 is maximum which is possible only if n2 is greater than n1 as well as n3. We will use nested-if statement to implement both if conditions.

The first if-statement states that if n3 is greater than n1 then inside this if-block, there is another if-statement which tests if n3 is greater than n2 and within this nested-if block print the message that value of n3 is maximum.

## Code 💻

``````// C Program to find maximum among 3 numbers

#include <stdio.h>
#include <conio.h>

void main()
{
int n1=0, n2=0, n3=0;
clrscr();
printf("Please enter the number 1: ");
scanf("%d", &n1);
printf("Please enter the number 2: ");
scanf("%d", &n2);
printf("Please enter the number 3: ");
scanf("%d", &n3);
if(n1 > n2) // outer condition
{
if(n1 > n3) // inner or nested condition
{
printf("\nValue of n1: %d is maximum.", n1);
}
}
if(n2 > n1) // outer condition
{
if(n2 > n3) // inner or nested condition
{
printf("\nValue of n2: %d is maximum.", n2);
}
}
else if(n3 > n1) // outer condition
{
if(n3 > n2) // inner or nested condition
{
printf("\nValue of n3: %d is maximum.", n3);
}
}
else
{
printf("\nEither 2 or all values are identical.");
}
getch();
}``````

## Example 😍

Let’s assume the user enters 3 values as 23, 49 and 44.

First, n1 will be compared with n2. Now, we know that 23 is lesser than 29 (23 > 49 returns false). Thus, it will not check the inner condition but the control will move to the next statement. Next, n2 will be compared with n1. Since, 49 is greater than 23 ( 49 > 23 returns true).

Next, it will check the inner condition and we know that 49 is also greater than 44. Thus, it will execute the inner condition’s statement and print the output.

This is how we find maximum number among 3 numbers.