C Program to Find Maximum of Three Numbers Using Nested If-Else
Complete guide with code, output, and step-by-step explanation of nested if-else logic
About the Author
Sankalan Data Technologies
Programming & Data Analytics Tutorials
Published by Sankalan Data Technologies. Our tutorials and data guides are independently developed and curated to provide practical, hands-on learning resources for Python, SQL, and data analytics practitioners.
Quick Answer: Maximum of Three Numbers in C
To find the maximum of three numbers in C using nested if-else, compare the first two numbers. Based on the result, compare the larger one with the third number.
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2) {
if (num1 >= num3)
printf("Maximum: %d\n", num1);
else
printf("Maximum: %d\n", num3);
} else {
if (num2 >= num3)
printf("Maximum: %d\n", num2);
else
printf("Maximum: %d\n", num3);
}
return 0;
}
Output:
Enter three numbers: 90 15 20
Maximum: 90
What You'll Learn in This Tutorial
- Purpose and Real-Life Applications
- Complete C Program
- Program Output
- Step-by-Step Explanation
- How the Logic Works
- Practice Questions
Purpose and Real-Life Applications
The purpose of this program is to demonstrate how nested if-else statements can be used to compare three values and determine the largest one. It's a fundamental exercise that helps beginners understand decision-making and multiple-level condition checking in C.
Real-Life Applications
- Student grading system — Determine the highest score on three tests
- Financial analysis — Compare the ROI of three investment options
- Sports analytics — Identify the athlete with the best performance stats
- Manufacturing — Compare three production lines for maximum efficiency
Complete C Program
C Program to Find Maximum of Three Numbers
/* C program to find maximum of three numbers
using nested if-else statements */
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers:\n");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2) {
if (num1 >= num3) {
printf("Maximum number is: %d\n", num1);
} else {
printf("Maximum number is: %d\n", num3);
}
} else {
if (num2 >= num3) {
printf("Maximum number is: %d\n", num2);
} else {
printf("Maximum number is: %d\n", num3);
}
}
return 0;
}
Program Output
Enter three numbers:
90
15
20
Maximum number is: 90
Step-by-Step Explanation
-
Declare variables: Three integer variables
num1,num2, andnum3are declared to store the user's input. -
Read input: The user is prompted to enter three numbers,
which are read using
scanf(). -
Outer if-else: The outer condition
if (num1 >= num2)checks whethernum1is greater than or equal tonum2. -
Inner if (true branch): If
num1 >= num2is true, we comparenum1withnum3. Ifnum1 >= num3,num1is the maximum; otherwise,num3is the maximum. -
Inner else (false branch): If the outer condition is false
(meaning
num2 > num1), we comparenum2withnum3. Ifnum2 >= num3,num2is the maximum; otherwise,num3is the maximum. -
Print result: The maximum value is displayed using
printf(). - Return: The program returns 0 to indicate successful execution.
How the Logic Works
Decision Tree
Is num1 >= num2?
│
├── YES → Is num1 >= num3?
│ │
│ ├── YES → Maximum = num1
│ │
│ └── NO → Maximum = num3
│
└── NO → Is num2 >= num3?
│
├── YES → Maximum = num2
│
└── NO → Maximum = num3
This decision tree shows that at most two comparisons are needed to determine the maximum of three numbers using nested if-else.
Why use >= instead of >? Using >= handles equal numbers correctly.
If two numbers are equal and both are the maximum, >= ensures that one of them
is still reported as the maximum.
Common Mistakes to Avoid
- Using > instead of >= — Can cause wrong output when numbers are equal
- Missing braces — Nested if-else without braces can lead to logic errors
- Wrong comparison order — Comparing num2 with num3 before comparing num1 with num2
- Missing & in scanf — Use
scanf("%d %d %d", &num1, &num2, &num3)
Practice Questions
Try these exercises to reinforce your understanding:
- Write a program to find the minimum of three numbers using nested if-else.
- Modify the program to find the maximum of four numbers.
- Write the same program using the ternary operator
? :. - Write the same program using
if-elseladder instead of nested if-else. - Sort three numbers in ascending order using nested if-else.
Hint: Use >= for comparisons. Draw the decision tree before writing code.
Why This Program Matters
- Foundation Concept: Nested if-else is essential for multi-level decision-making.
- Exam Relevance: Frequently asked in BCA, MCA, and engineering exams.
- Real-World Use: Used in grading systems, financial analysis, and more.
- Interview Preparation: Common coding interview question.
Frequently Asked Questions
How do I find the maximum of three numbers in C using nested if-else?
First compare num1 with num2. If num1 is greater or equal, then compare num1 with num3. Otherwise compare num2 with num3. Print the largest value.
What is a nested if-else statement in C?
A nested if-else is an if-else statement placed inside another if or else block. It allows multiple levels of condition checking, which is useful for comparing three or more values.
Can I find the maximum of three numbers without nested if-else?
Yes. You can use logical AND (&&) in a single if statement, use the ternary operator, or use the fmax() function from math.h. However, nested if-else is the most readable for beginners.
What is the output when the user enters 90, 15, 20?
The program outputs: Maximum number is: 90. It compares all three numbers using nested if-else and prints the largest one.
Why do we use >= instead of > in the maximum program?
Using >= handles equal numbers correctly. If two numbers are equal and both are the maximum, >= ensures that one is still reported as the maximum.
What are real-life applications of this program?
Applications include student grading systems, financial analysis (best ROI), sports analytics (best performance), and manufacturing (most efficient production line).
Related Tutorials
Previous Topic: Check if a Number is Positive, Negative, or Zero | Next Topic: Convert Temperature Using Switch Case