# C program to find maximum between three numbers

Write a C program to find maximum between three numbers using ladder if else or nested if. How to find maximum or minimum between three numbers using if else in C programming. Logic to find maximum or minimum between three numbers in C program.

Example
Input

```Input num1: 10
Input num2: 20
Input num3: 15```

Output

`Maximum is: 20`

## Logic to find maximum between three numbers

In previous program we learned to find maximum between two numbers. That was pretty easy. In this program we will continue our discussion and we will write program to find maximum between three numbers.

Step by step descriptive logic to find maximum between three numbers.

1. Input three numbers from user. Store it in some variable say num1, num2 and num3.
2. Compare first two numbers i.e. `num1 > num2`. If the statement is `true` then num2 is surely not max value. Perform one more comparison between num1 with num3 i.e. `if(num1 > num3)`, then num1 is max otherwise num3.
3. If the statement `num1 > num2` is `false`. Which indicates that num1 is not max. Hence, this time compare num2 with num3. If the statement `num2 > num3` is `true` then num2 is max otherwise num3.

## Program to find maximum between three number using nested if

``````/**
* C program to find maximum between three numbers using nested if
*/

#include <stdio.h>

int main()
{
int num1, num2, num3, max;

/* Input three numbers from user */
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);

if(num1 > num2)
{
if(num1 > num3)
{
/* If num1 > num2 and num1 > num3 */
max = num1;
}
else
{
/* If num1 > num2 but num1 > num3 is not true */
max = num3;
}
}
else
{
if(num2 > num3)
{
/* If num1 is not > num2 and num2 > num3 */
max = num2;
}
else
{
/* If num1 is not > num2 and num2 > num3 */
max = num3;
}
}

/* Print maximum value */
printf("Maximum among all three numbers = %d", max);

return 0;
}``````

The above approach is lengthy and not recommended for this problem. You can combine relational and logical operator along with ladder `if...else...if` to find maximum in more simple way.

## Logic to find maximum using ladder `if...else...if`

Instead of using nested if else. You can combine two or more conditions together using logical operators. A number num1 among three numbers num1, num2 and num3 is said maximum if `num1 > num2 and num1 > num3`.

Here we will use logical AND `&&` operator to combine two conditions together. Maximum between three numbers is determined by three cases.

• num1 is maximum if `num1 > num2 and num1 > num3`.
• num2 is maximum if `num2 > num1 and num2 > num3`.
• num3 is maximum if `num3 > num1 and num3 > num2`.

Let us implement this using logical operator and ladder if else.

## Program to find maximum using ladder `if...else...if`

``````/**
* C program to find maximum between three numbers using ladder if else
*/

#include <stdio.h>

int main()
{
int num1, num2, num3, max;

/* Input three numbers from user */
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);

if((num1 > num2) && (num1 > num3))
{
/* If num1 is greater than both */
max = num1;
}
else if((num2 > num1) && (num2 > num3))
{
/* If num2 is greater than both */
max = num2;
}
else if((num3 > num1) && (num3 > num2))
{
/* If num3 is greater than both */
max = num3;
}

/* Print maximum number */
printf("Maximum among all three numbers = %d", max);

return 0;
}``````

The above approach was short and little easy to understand. However, we are unnecessarily checking six conditions. You can further short the logic using below approach.

## Program to find maximum between three numbers using ladder `if...else...if`

``````/**
* C program to find maximum between three numbers using ladder if else if
*/

#include <stdio.h>

int main()
{
int num1, num2, num3, max;

/* Input three numbers from user */
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);

if((num1 > num2) && (num1 > num3))
{
/* If num1 > num2 and num1 > num3 */
max = num1;
}
else if(num2 > num3)
{
/* If num1 is not > num2 and num2 > num3 */
max = num2;
}
else
{
/* If num1 is not > num2 and num2 is also not > num3 */
max = num3;
}

/* Print maximum number */
printf("Maximum among all three numbers = %d", max);

return 0;
}``````

That was really awesome trick. Advance your skills further. Learn this program using conditional operator.

Output

```Enter three numbers: 10
50
120
Maximum among all three numbers = 120```

Happy coding 😉 