Write a C program to find maximum between two numbers using if else. C program to input 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.
Input
Input num1: 10 Input num2: 20
Output
Maximum = 20
Required knowledge
Basic C programming, Relational operators, If statement
Logic to find minimum or maximum between two numbers
Finding maximum in general is comparison of two numbers. In C programming we compare two quantities using relational operator. We use either >
or <
operator to compare two numbers (or other primitive types). Relational operator evaluates 1 (true
) or 0 (false
) depending on condition.
We can write expression to find maximum between num1 and num2 as num1 > num2
. The expression num1 > num2
evaluate 1 if num1 is greater than num2, otherwise evaluates 0.
After finding maximum, we need to execute some action based on the maximum i.e. print the maximum number. In C if...else
provides ability to execute an action based on condition. So we will make use of relational operator along with if...else
to find maximum.
Below is step by step descriptive logic to find maximum.
- Input two numbers from user. Store it in some variable say num1 and num2.
- Check
if(num1 > num2)
then print num1 is maximum. - Check
if(num2 > num1)
then print num2 is maximum. - Check
if(num1 == num2)
then both the numbers are equal.
Program to find maximum using simple if
/**
* C program to find maximum between two numbers
*/
#include <stdio.h>
int main()
{
int num1, num2;
/* Input two numbers from user */
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
/* If num1 is maximum */
if(num1 > num2)
{
printf("%d is maximum", num1);
}
/* If num2 is maximum */
if(num2 > num1)
{
printf("%d is maximum", num2);
}
/* Additional condition check for equality */
if(num1 == num2)
{
printf("Both are equal");
}
return 0;
}
The above approach to check maximum between two numbers is easy to understand. However, instead of writing three conditions you can use if…else statement.
Program to find maximum between two numbers using if...else
/**
* C program to find maximum between two numbers
*/
#include <stdio.h>
int main()
{
int num1, num2;
/* Input two numbers from user */
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
/* Compare num1 with num2 */
if(num1 > num2)
{
/* True part means num1 > num2 */
printf("%d is maximum", num1);
}
else
{
/* False part means num1 < num2 */
printf("%d is maximum", num2);
}
return 0;
}
You can also use a max variable. Assign maximum in the max variable based on if...else
condition. Finally print the value of max.
In addition, as you can see in above programs if
or else
body contains only single statement. Hence, you can ignore braces {
}
after if
and else
statement.
Program to find maximum between two numbers
/**
* C program to find maximum between two numbers
*/
#include <stdio.h>
int main()
{
int num1, num2, max;
/* Input two numbers from user */
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
/* Compare num1 with num2 */
if(num1 > num2)
max = num1;
else
max = num2;
printf("%d is maximum.", max);
return 0;
}
Advance you skills by learning other approaches to find maximum between two numbers.
Learn more –
Output
Enter two numbers: 10 12 12 is maximum
Happy coding 😉
Recommended posts
- If else programming exercises index.
- C program to find maximum between three numbers.
- C program to check whether a number is even or odd.
- C program to check whether a number is divisible by 5 and 11 or not.
- C program to check whether a number is negative, positive or zero.
- C program to check whether a triangle is valid or not if all angles are given.
- C program to find maximum and minimum element in an array.