C program to find maximum between three numbers

Write a C program to find maximum number between three numbers using ladder if or nested if. How to find maximum or minimum between three numbers using if else in C programming. Finding maximum between three numbers using if statement in C program. 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

Required knowledge

Basic C programming, If else, Operators

Logic to find maximum between three numbers

In previous program we learnt to find maximum between two numbers.

Below is the 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 num1 > num2. If the statement is true. Then we need to check two more numbers for maximum i.e. num1 > num3. If again it evaluates to true. Then num1 is maximum otherwise num2.
  3. Suppose the statement num1 > num2 is false. Which means either num2 or num3 is maximum. Now check one more condition, if num2 > num3. It evaluates true if num2 is maximum otherwise num3 is maximum.

There are various way to code this program. First let us code this program using nested if.

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, maximum;

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

    if(num1 > num2)
    {
        if(num1 > num3)
        {
            maximum = num1;
        }
        else
        {
            maximum = num3;
        }
    }
    else
    {
        if(num2 > num3)
        {
            maximum = num2;
        }
        else
        {
            maximum = num3;
        }
    }
    
    /* Prints the maximum value */
    printf("Maximum among all three numbers = %d", maximum);

    return 0;
}

The above method is lengthy and not recommended. Below is the simplest and recommended for these type of programs. It uses the relational as well as logical operator to find maximum.

Logic to find maximum using logical operator

Instead of using nested if else. You can also combine two or more conditions together using logical operator. Here we will use logical AND operator to combine two conditions together. Maximum between three numbers is determined by three cases.

  • num1 is maximum only and only if num1 > num2 and num1 > num3.
  • Similarly num2 is maximum only and only if num2 > num1 and num2 > num3.
  • Finally num3 is maximum only if both the conditions num3 > num1 and num3 > num2 are true.

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

Program to find maximum between three numbers using logical operator

/**
 * C program to find maximum between three numbers
 */

#include <stdio.h>

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

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


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

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

    return 0;
}

Another simple and easiest approach of coding this program is mentioned below.

Program to find maximum using logical operator 2

/**
 * C program to find maximum between three numbers
 */

#include <stdio.h>

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

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


    if((num1 > num2) && (num1 > num3))
    {
        maximum = num1;
    }
    else if(num2 > num3)
    {
        maximum = num2;
    }
    else
    {
        maximum = num3;
    }

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

    return 0;
}

Advance your skills by learning this program using another approach.

Read more - Program to find maximum between three numbers using conditional operator.

Output

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

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>