C program to find maximum between two numbers

Write a C program to find maximum between two numbers using if else. C program to enter two numbers from user and find maximum between two numbers using if else. How to find maximum or minimum between two numbers using if else in C programming.

Example:
Input first number: 2
Input second number: 23
Output maximum: 23

Required knowledge

Basic C programming, Use of Relational operators, If else

Program to find maximum or minimum

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

#include <stdio.h>

int main()
{
    int num1, num2;

    /*
     * Reads any two integer values from user
     */
    printf("Enter any two numbers:\n");
    scanf("%d %d", &num1, &num2);

    /*
     * Check if num1 > num2 or not and prints the maximum
     */
    if(n1 > n2)
    {
        printf("%d is maximum", num1);        
    }
    else
    {
        printf("%d is maximum", num2);
    }

    return 0;
}

Note: If you want to find minimum between two numbers you just need to reverse the sign of if statement i.e. you can write it as if(n1 < n2) for checking minimum.

Also advance your programming skills by learning this program using other approaches -
C program to find maximum between two numbers using switch case.
C program to find maximum between two numbers using conditional/ternary operator.
C program to find maximum or minimum between two numbers using functions.

Output
Enter any two numbers:
10
12
12 is maximum

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