Write a C program to find maximum between three numbers

C PROGRAMMING

Write a C program to find maximum between three numbers.

C program to find maximum between three numbers

CategoriesC programming5 mins read

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

 

Required knowledge

Basic C programmingRelational operatorsLogical operatorsIf else

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. https://codeforwin.org/c-programming/c-program-to-find-maximum

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

  1. Input three numbers from user. Store it in some variable say num1num2 and num3.
  2. Compare first two numbers i.e. num1 > num2. If the statement is true then num2 is surely not max value. num3 i.e.
  3. If the statement num1 > num2 is false. Which indicates that num1 is not max. Hence, this time compare num2 with 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 num1num2 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.

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 approa

 

				
					#include<stdio.h>

void main()
{
    //Declare two integers
    int a,b,c;
    
    //Input the two integers
    printf("Enter Integer 1:");
    scanf("%d",&a);
    
    printf("Enter Integer 2:");
    scanf("%d",&b);
    
    printf("Enter Integer 3:");
    scanf("%d",&c);
    
    //Use if - else  to compare the two integers
    
    if( a > b && a > c )
    {
        printf("Integer 1 is the greatest");
    }
    
    if (b > a && b > c)
    {
        printf("Integer 2 is the greatest");
    }
    
    if( c > a && c > b)
    {
        printf("Integer 3 is the greatest");
    }
    
}
				
			

Go Back

Join To Get Our Newsletter
Spread the love