Largest of Three Numbers: flowchart, algorithm and program
This program reads three integers a, b and c and prints the largest one. It uses nested if / else: first compare a with b, then compare the winner with c, so only two comparisons are needed.
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read a, b, c.
- Step 3: If a >= b, go to step 4; otherwise go to step 5.
- Step 4: If a >= c, go to step 6; otherwise go to step 7.
- Step 5: If b >= c, go to step 8; otherwise go to step 9.
- Step 6: Print a. Go to step 10.
- Step 7: Print c. Go to step 10.
- Step 8: Print b. Go to step 10.
- Step 9: Print c.
- Step 10: Stop.
Pseudocode
BEGIN
READ a, b, c
IF a >= b THEN
IF a >= c THEN
PRINT a
ELSE
PRINT c
END IF
ELSE
IF b >= c THEN
PRINT b
ELSE
PRINT c
END IF
END IF
END
Program in C, Python and Java
Largest of Three Numbers program in C
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b) {
if (a >= c) {
printf("Largest = %d\n", a);
} else {
printf("Largest = %d\n", c);
}
} else {
if (b >= c) {
printf("Largest = %d\n", b);
} else {
printf("Largest = %d\n", c);
}
}
return 0;
}Largest of Three Numbers program in Python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b:
if a >= c:
print("Largest =", a)
else:
print("Largest =", c)
else:
if b >= c:
print("Largest =", b)
else:
print("Largest =", c)Largest of Three Numbers program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if (a >= b) {
if (a >= c) {
System.out.println("Largest = " + a);
} else {
System.out.println("Largest = " + c);
}
} else {
if (b >= c) {
System.out.println("Largest = " + b);
} else {
System.out.println("Largest = " + c);
}
}
sc.close();
}
}
Sample input: a = 12, b = 45, c = 30
Output: Largest = 45
How it works
- scanf reads the three numbers into a, b and c.
- The outer check a >= b decides which of the two is bigger. If it is true, a is the candidate; otherwise b is the candidate.
- The inner check compares that candidate with c. In the a >= b branch we test a >= c; in the else branch we test b >= c.
- If the candidate loses to c, then c is the largest. Otherwise the candidate itself is printed.
- Dry run with a = 12, b = 45, c = 30: a >= b is false (12 < 45), so we go to the else branch. b >= c is true (45 >= 30), so it prints Largest = 45.
Dry run (trace table)
| Step | Line | Box | a | b | c | Output |
|---|---|---|---|---|---|---|
| 1 | 3 | Start | ||||
| 2 | 6 | Read a, b, c | 12 | 45 | 30 | Enter three numbers: |
| 3 | 8 | a >= b? | 12 | 45 | 30 | |
| 4 | 15 | b >= c? | 12 | 45 | 30 | |
| 5 | 16 | Print b | 12 | 45 | 30 | Largest = 45 |
| 6 | 21 | Stop | 12 | 45 | 30 |
Time and space complexity
Time: O(1) · Space: O(1). Only two comparisons are made regardless of the input values, using a fixed number of variables.
Common mistakes
- Comparing only a with b and a with c separately, and forgetting to compare b with c in the else branch.
- Using > instead of >= without thinking about equal numbers. The result is still correct here, but you should know which variable gets printed when values are equal.
- Forgetting the &, as in scanf("%d", a) instead of &a, so the input is not stored properly.
Viva questions
Q1. What does this program do, and what is the idea behind the nested if-else?
It reads three integers and prints the largest. First it compares a and b to find the bigger of the two, then it compares that winner with c, so only two comparisons are needed.
Q2. What does the condition `a >= b` on line 8 decide?
It decides which of a or b is the larger one. If it is true, a is the candidate and is then compared with c; otherwise b is the candidate and is compared with c.
Q3. Why did you use `>=` and not `>`? What happens if two or all three numbers are equal?
With `>=`, ties still go to a valid branch and the program prints a correct value. If all three are equal, say 5 5 5, it goes a>=b, then a>=c, and prints a, which is the same as the largest. With `>` it would also work, because the else branches still print an equal value.
Q4. Why is `&` used before a, b and c in scanf?
scanf needs the address of each variable so it can store the input value there. Without `&` it would try to use the variable's value as an address and the program would crash or misbehave.
Q5. What is printed for inputs 0, -5, -2? Does the program work with negative numbers?
It prints Largest = 0. The program only compares values and does not assume positive numbers, so negatives and zero work correctly; for -5 -2 -9 it prints -2.
Q6. Why does line 18 print c, and how many comparisons are made in the worst case?
Line 18 is reached when b is greater than a and c is greater than b, so c is the largest. Exactly two comparisons are made on any path, so time complexity is O(1) constant time.
Q7. Can the same be done without nested ifs? Show a different approach.
Yes. Use a variable max = a; then `if (b > max) max = b;` and `if (c > max) max = c;`. Alternatively use a single condition with logical AND, such as `if (a >= b && a >= c)`, followed by else-if for b and an else for c.
Q8. What would change if you had to find the largest of four numbers, or also print the smallest?
Nested ifs would grow to many more branches, so it is better to keep a running max in a variable and compare each new number with it, or use a loop with an array for n numbers. For the smallest, you would flip the comparisons or keep a separate min variable.
Frequently asked questions
How many comparisons does this program make to find the largest of three numbers?
Exactly two: one between a and b, and one between the winner and c. Every path through the nested if / else runs only two conditions.
What happens if two or all three numbers are equal?
Because the program uses >=, it still prints the correct largest value. For example, if a = b = c = 5, the condition a >= b and a >= c are both true and it prints 5.
Can I find the largest of three numbers without nested if?
Yes. You can use logical operators like if (a >= b && a >= c), or the ternary operator, or keep a variable max and update it. Nested if / else is just the clearest way to show the decision steps.