“Input three numbers and display the largest and the smallest number” is one of the first decision-making programs in Class 11 and 12 computer science. It teaches you how a program compares values with if statements and how to keep the best answer found so far in a variable. In this post you will see the program in Python, C and Java, a step-by-step dry run, five other ways to write it, and answers to common questions. Use the interactive tool below to try your own numbers and watch every comparison.
Type three numbers and watch the program find the largest and the smallest one step by step.
Graphical view
Code
Output
What You Will Learn
- The problem statement
- The idea: keep the best so far
- The program in Python, C and Java
- Dry run with examples
- Other ways: if-elif, max() and min(), sort, one-line input
- Common mistakes, practice questions and FAQ
Problem Statement
Input three numbers and display the largest and the smallest number.
The user types three whole numbers. The program prints the biggest of them and the smallest of them. The interactive tool above accepts whole numbers from -999 to 999, including negative numbers and numbers that are equal.
The Idea: Keep the Best So Far
Think of a quiz where three students step forward one at a time. You first accept the first student as the tallest. When the second student comes, you compare: if he is taller, he becomes the tallest. Then the third student does the same. After three students you know the tallest, and you never needed to compare every pair.
The program does exactly this with two boxes (variables):
largestarts asa. Ifb > large, thenlarge = b. Ifc > large, thenlarge = c.smallstarts asa. Ifb < small, thensmall = b. Ifc < small, thensmall = c.
Only two comparisons are needed for each answer, so the whole program makes four comparisons. The same idea works for 3 numbers or 3,000 numbers; with many numbers it simply sits inside a loop.
The Program
Logic:
- Read the three numbers a, b and c as integers with
int(). - Set
large = a, then update it using b and c. - Set
small = a, then update it using b and c. - Print both answers.
Python program
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
large = a
if b > large:
large = b
if c > large:
large = c
print("Largest number:", large)
small = a
if b < small:
small = b
if c < small:
small = c
print("Smallest number:", small)
Output
Enter first number: 7
Enter second number: 3
Enter third number: 9
Largest number: 9
Smallest number: 3
C program
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Enter third number: ");
scanf("%d", &c);
int large = a;
if (b > large) {
large = b;
}
if (c > large) {
large = c;
}
printf("Largest number: %d\n", large);
int small = a;
if (b < small) {
small = b;
}
if (c < small) {
small = c;
}
printf("Smallest number: %d\n", small);
return 0;
}
Enter first number: 7
Enter second number: 3
Enter third number: 9
Largest number: 9
Smallest number: 3
In C, scanf("%d", &a) needs the address of the variable, which is why the & sign is used.
Java program
import java.util.Scanner;
public class LargestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
System.out.print("Enter third number: ");
int c = sc.nextInt();
int large = a;
if (b > large) {
large = b;
}
if (c > large) {
large = c;
}
System.out.println("Largest number: " + large);
int small = a;
if (b < small) {
small = b;
}
if (c < small) {
small = c;
}
System.out.println("Smallest number: " + small);
}
}
Enter first number: 7
Enter second number: 3
Enter third number: 9
Largest number: 9
Smallest number: 3
Save this file as LargestOfThree.java, because the public class name must match the file name.
Dry Run
Let us run the program by hand for a = 7, b = 3, c = 9.
| Step | Statement | Condition | large | small |
|---|---|---|---|---|
| 1 | large = a | 7 | ||
| 2 | if b > large | 3 > 7 is false | 7 | |
| 3 | if c > large | 9 > 7 is true, large = c | 9 | |
| 4 | small = a | 9 | 7 | |
| 5 | if b < small | 3 < 7 is true, small = b | 9 | 3 |
| 6 | if c < small | 9 < 3 is false | 9 | 3 |
The program prints Largest number: 9 and Smallest number: 3. Now try a = 10, b = 2, c = 6: large stays 10 because neither 2 nor 6 is bigger, and small becomes 2 when b is checked and stays 2 when c is checked. Negative numbers work in the same way. For -4, -9, -1 the largest is -1, because -1 is closer to zero than -4 and -9.
Enter first number: -4
Enter second number: -9
Enter third number: -1
Largest number: -1
Smallest number: -9
When all numbers are equal, no comparison is true, so both answers stay equal to the first number:
Enter first number: 5
Enter second number: 5
Enter third number: 5
Largest number: 5
Smallest number: 5
Other Ways to Write the Program
Method 2: if-elif-else with and
Instead of keeping a running answer, you can test each number against the other two. We use >= so that equal numbers are handled correctly.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Largest number:", a)
elif b >= a and b >= c:
print("Largest number:", b)
else:
print("Largest number:", c)
if a <= b and a <= c:
print("Smallest number:", a)
elif b <= a and b <= c:
print("Smallest number:", b)
else:
print("Smallest number:", c)
For 8, 8, 3 this prints the following. Note that the first matching branch wins, so the answer is correct even though two numbers tie:
Enter first number: 8
Enter second number: 8
Enter third number: 3
Largest number: 8
Smallest number: 3
This method is easy to read, but it needs six comparisons and becomes long when there are more numbers.
Method 3: Built-in max() and min()
Python already knows how to do this. This is the shortest way, but your teacher may expect the comparison logic in an exam.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Largest number:", max(a, b, c))
print("Smallest number:", min(a, b, c))
Method 4: Sort a list
Put the three numbers in a list and sort it. After sorting, the first item is the smallest and the last item is the largest.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
nums = [a, b, c]
nums.sort()
print("Largest number:", nums[2])
print("Smallest number:", nums[0])
Method 5: Conditional expression
Python’s one-line x if condition else y expression does the same work as a small if statement.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
large = a if a > b else b
large = c if c > large else large
small = a if a < b else b
small = c if c < small else small
print("Largest number:", large)
print("Smallest number:", small)
Method 6: Read all three numbers in one line
The split() method cuts the typed line into words and map(int, ...) converts each word to an integer, so the user can type 7 3 9 on a single line.
a, b, c = map(int, input("Enter three numbers: ").split())
print("Largest number:", max(a, b, c))
print("Smallest number:", min(a, b, c))
Enter three numbers: 7 3 9
Largest number: 9
Smallest number: 3
C and Java: one-line input and built-in helpers
C can read three numbers with a single scanf:
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
int large = a, small = a;
if (b > large) large = b;
if (c > large) large = c;
if (b < small) small = b;
if (c < small) small = c;
printf("Largest number: %d\n", large);
printf("Smallest number: %d\n", small);
return 0;
}
Java has Math.max and Math.min, which take two numbers, so we nest them for three:
import java.util.Scanner;
public class LargestMath {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
System.out.println("Largest number: " + Math.max(a, Math.max(b, c)));
System.out.println("Smallest number: " + Math.min(a, Math.min(b, c)));
}
}
Save the Java program as LargestMath.java.
Variation: find the middle number
The middle number is the one that is neither the largest nor the smallest. Because the three numbers add up to large + middle + small, the middle number is the total minus the other two. This also works when two numbers are equal.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
large = max(a, b, c)
small = min(a, b, c)
print("Middle number:", a + b + c - large - small)
Enter first number: 7
Enter second number: 3
Enter third number: 9
Middle number: 7
Variation: decimal numbers
Replace int() with float() to accept numbers with a decimal point. In C use float and %f, and in Java use double and nextDouble().
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
c = float(input("Enter third number: "))
print("Largest number:", max(a, b, c))
print("Smallest number:", min(a, b, c))
Enter first number: 2.5
Enter second number: -1.5
Enter third number: 7.25
Largest number: 7.25
Smallest number: -1.5
Common Mistakes
- Forgetting int(). Without it,
input()returns text, and Python compares text letter by letter. Then “9” is bigger than “10” because “9” comes after “1” in the alphabet. - Starting with 0. Writing
large = 0gives a wrong answer when all numbers are negative, because 0 is bigger than all of them. Always start with one of the real numbers. - Using only if-else with two tests. Checking just
a > band thena > cin separate, unconnected blocks can print two answers or none. Useelifor a running variable. - Using > when you need >=. In Method 2 a strict
>misses the case where two numbers tie for the largest, and the final else branch may print a wrong number (for 8, 8, 3 it would print 3). - Mixing up the signs. The largest uses
>and the smallest uses<. Copying the largest block and forgetting to flip the sign is the most common error. - Wrong Java file name. A public class must be saved in a file with the same name, such as
LargestOfThree.java.
Practice Questions
- Change the program to print the largest of four numbers. How many comparisons do you need?
- Write a program that reads three numbers and prints them in ascending order without using
sort(). - Print the difference between the largest and the smallest number.
- Read three marks and print which subject has the highest marks, using an if-elif ladder.
- Read n numbers in a loop and print the largest and smallest. Hint: start both with the first number.
- Find the second largest of three numbers.
Frequently Asked Questions
How do you find the largest of three numbers in Python?
Read the three numbers with int(input()), set large = a, and then replace it with b or c whenever they are bigger. You can also write max(a, b, c) for a short answer.
How do you find the smallest of three numbers?
Use the same method with the opposite sign. Set small = a, replace it with b if b < small, and replace it with c if c < small. In Python, min(a, b, c) does it in one step.
How many comparisons are needed to find the largest of three numbers?
Two comparisons are enough: one for b and one for c. Finding both the largest and the smallest with this method takes four comparisons.
What happens if two or all three numbers are equal?
The program still gives the correct answer. If all three are equal, the largest and the smallest are the same number. The running-variable method uses strict signs, so it simply keeps the first of the equal numbers, which has the same value.
Does the program work for negative numbers?
Yes. The comparison operators work on negative values too, and -1 is larger than -4. The program only fails if you start large at 0 instead of at one of the input numbers.
What is the difference between if-elif-else and a series of if statements?
With if-elif-else, Python stops at the first true condition, so only one branch runs. With separate if statements, every condition is tested and several blocks may run. The running-variable method uses separate ifs on purpose, because each one may update the same variable.
Why do I get a wrong answer like 9 being bigger than 10?
You compared strings instead of numbers. input() always returns text, so convert it with int() or float() before comparing.
Can I find the largest of three numbers without using if statements?
Yes. In Python use max(), in Java use Math.max(), or sort a list and take the last item. In an exam, show the if-based logic unless the question allows built-in functions.
How do I extend this program to n numbers?
Read the first number into both large and small, then use a loop for the remaining numbers. Inside the loop, use one if to update large and one if to update small. A list with max(numbers) and min(numbers) works too.
What is the time complexity of this program?
For three numbers the work is constant. For n numbers the program makes about 2n comparisons, so the time complexity is O(n), and it uses only a few variables, so the extra space is O(1).