C program to find maximum between three numbers

Write a C program to find maximum number between three numbers using if else. How to find maximum or minimum between three numbers using if else in C programming. Finding maximum between three numbers using if else in C program.

Example:
Input first number: 10
Input second number: 50
Input third number: 120
Output maximum: 120

Required knowledge

Basic C programming, If else

Logic to find maximum between three numbers

In previous program we had already seen how to find maximum/minimum between two numbers using relational > operator.
Here in this program we will be using the relational operator in combination with logical AND && to find maximum between three numbers. In order to find maximum between three numbers num1, num2 and num3, there must be two condition to be satisfied for any number to be maximum i.e. the number must be greater than other two number. Which in terms if num1 is maximum then it must satisfy both conditions num1 > num2 and num1 > num3 and to combine these two conditions into one we will use logical AND && operator.

Program to find maximum between three numbers

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

#include <stdio.h>

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

    /*
     * Reads three numbers from user
     */
    printf("Enter three numbers to find maximum: \n");
    scanf("%d%d%d", &num1, &num2, &num3);


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

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

    return 0;
} 


Program to find maximum between three numbers using nested if

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

#include <stdio.h>

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

    /* Reads three numbers from user */
    printf("Enter three numbers to find maximum: \n");
    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("\nMaximum among all three numbers = %d", maximum);

    return 0;
} 


Also advance your skill by learning to find maximum between more than three numbers using functions.


Output
Enter three numbers to find maximum: 10
50
120

Maximum among all three numbers = 120

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment