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 exercise we learnt to find maximum between two numbers.
Now, think logically if I gave a task to find maximum between three numbers a, b and c. How will you find that? Let us get the step by step descriptive logic to find maximum between three numbers.
- Compare first two numbers a > b. If the statement is true. Then we only need to check two more numbers for maximum i.e. a > c. If again it evaluates to true. Then a is maximum otherwise c.
- Suppose the statement a > b is false. Which means either b or c is maximum. Now check one more condition b > c. It evaluates to true if b is maximum otherwise c.
There are various way to code this program. First let us observe 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 method to use for these types of program. It uses the relational as well as logical operator to find maximum.
Logic to find maximum using logical operator
We already know how to find maximum between two numbers using relational operator. Now for three numbers observe the case. For given three numbers say a, b and c.
- a is maximum only and only if a > b and a > c. If both statements are true then only a is maximum.
- Like above case b is maximum only and only if b > a and b > c.
- Finally c is maximum only if both the conditions c > a and c > b are true.
Let us not 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\n", maximum); return 0; }
Another approach that will do the trick is mentioned below. I am leaving the logic of below to you. Hope that it would be easy to get the concept of below program.
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\n", maximum); return 0; }
Advance your skills by learning this program using conditional operator.
Enter three numbers: 10 50 120 Maximum among all three numbers = 120
Happy coding ;)
You may also like
- If else programming exercises index.
- C program to find maximum between two numbers using switch case.
- C program to find maximum between two numbers using conditional operator.
- C program to check leap year.
- C program to check whether a number is positive, negative or zero.
- C program to enter sides of a triangle and check whether it is valid triangle or not.
- C program to enter week number and print week day name.