“Input two numbers and display the larger and the smaller number” is usually the very first if statement a student writes. It teaches how a program compares two values and chooses what to do next. In this post you will see the program in Python, C and Java, a dry run, six other ways to write it, and answers to common questions. Use the interactive tool below to try your own numbers and watch the comparison happen step by step.
Type two numbers and watch the program find the larger and the smaller one step by step.
Graphical view
Code
Output
What You Will Learn
- The problem statement
- The idea behind the program
- The program in Python, C and Java
- Dry run with examples
- Other ways: if-else, max() and min(), ternary, sort, formula
- Common mistakes, practice questions and FAQ
Problem Statement
Input two numbers and display the larger and the smaller number.
The user types two whole numbers. The program prints the bigger one and the smaller one. The interactive tool above accepts whole numbers from -999 to 999, including negative numbers, zero and equal numbers.
The Idea Behind the Program
Imagine two boxes, large and small. Put the first number in both of them, because with only one number seen so far it is both the biggest and the smallest. Then look at the second number:
- If
b > large, the second number is bigger, so replace the contents oflargewith b. - If
b < small, the second number is smaller, so replace the contents ofsmallwith b.
Only two comparisons are needed in total. The same “keep the best so far” idea later grows into the programs for three numbers and for whole lists.
The Program
Logic:
- Read a and b as integers with
int(). - Set
large = a. Ifb > large, setlarge = b, then print it. - Set
small = a. Ifb < small, setsmall = b, then print it.
Python program
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
large = a
if b > large:
large = b
print("Larger number:", large)
small = a
if b < small:
small = b
print("Smaller number:", small)
Output
Enter first number: 7
Enter second number: 3
Larger number: 7
Smaller number: 3
C program
#include <stdio.h>
int main() {
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
int large = a;
if (b > large) {
large = b;
}
printf("Larger number: %d\n", large);
int small = a;
if (b < small) {
small = b;
}
printf("Smaller number: %d\n", small);
return 0;
}
Enter first number: 7
Enter second number: 3
Larger number: 7
Smaller 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 LargerOfTwo {
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();
int large = a;
if (b > large) {
large = b;
}
System.out.println("Larger number: " + large);
int small = a;
if (b < small) {
small = b;
}
System.out.println("Smaller number: " + small);
}
}
Enter first number: 7
Enter second number: 3
Larger number: 7
Smaller number: 3
Save this file as LargerOfTwo.java, because the public class name must match the file name.
Dry Run
Run the program by hand for a = 7, b = 3.
| Step | Statement | Condition | large | small |
|---|---|---|---|---|
| 1 | large = a | 7 | ||
| 2 | if b > large | 3 > 7 is false | 7 | |
| 3 | small = a | 7 | 7 | |
| 4 | if b < small | 3 < 7 is true, small = b | 7 | 3 |
So the output is Larger number: 7 and Smaller number: 3. If the numbers are the other way round, a = 4, b = 12, the first condition 12 > 4 is true and large becomes 12, while small stays 4:
Enter first number: 4
Enter second number: 12
Larger number: 12
Smaller number: 4
With negative numbers the rule is the same: -2 is larger than -5 because it is closer to zero.
Enter first number: -5
Enter second number: -2
Larger number: -2
Smaller number: -5
When both numbers are equal, neither condition is true, so both answers are that same number:
Enter first number: 6
Enter second number: 6
Larger number: 6
Smaller number: 6
Other Ways to Write the Program
Method 2: if-else
Compare the two numbers once and print both answers in each branch. This needs only one comparison.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print("Larger number:", a)
print("Smaller number:", b)
else:
print("Larger number:", b)
print("Smaller number:", a)
When the numbers are equal, a > b is false, so the else branch runs. It still prints the correct values because both numbers are the same. If you want a special message for equal numbers, add an elif:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print("Larger number:", a)
print("Smaller number:", b)
elif b > a:
print("Larger number:", b)
print("Smaller number:", a)
else:
print("Both numbers are equal:", a)
Enter first number: 6
Enter second number: 6
Both numbers are equal: 6
Method 3: Built-in max() and min()
Python already has functions for this. They are the shortest way, but in an exam the question may expect you to show the comparison.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Larger number:", max(a, b))
print("Smaller number:", min(a, b))
Method 4: Conditional expression
Python’s x if condition else y is a one-line if-else that gives a value.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
large = a if a > b else b
small = a if a < b else b
print("Larger number:", large)
print("Smaller number:", small)
Method 5: Sort a list
Put the numbers in a list and sort it. 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: "))
nums = [a, b]
nums.sort()
print("Larger number:", nums[1])
print("Smaller number:", nums[0])
Method 6: Read both numbers in one line
split() cuts the typed line into words, and map(int, ...) turns each word into an integer, so the user can type 7 3 on one line.
a, b = map(int, input("Enter two numbers: ").split())
print("Larger number:", max(a, b))
print("Smaller number:", min(a, b))
Enter two numbers: 7 3
Larger number: 7
Smaller number: 3
Method 7: A formula with abs()
The absolute difference abs(a - b) is the distance between the two numbers. If you add that distance to the sum and halve it, you get the larger number. If you subtract it and halve it, you get the smaller number. This works without any if statement.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
large = (a + b + abs(a - b)) // 2
small = (a + b - abs(a - b)) // 2
print("Larger number:", large)
print("Smaller number:", small)
Enter first number: -5
Enter second number: -2
Larger number: -2
Smaller number: -5
We use // (integer division) so that the answer stays an integer. The sum plus or minus the distance is always even, so no digit is lost.
C and Java: the ternary operator and Math.max
C has the conditional operator ? : and can read two numbers with one scanf:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
int large = (a > b) ? a : b;
int small = (a < b) ? a : b;
printf("Larger number: %d\n", large);
printf("Smaller number: %d\n", small);
return 0;
}
Java provides Math.max and Math.min:
import java.util.Scanner;
public class LargerMath {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Larger number: " + Math.max(a, b));
System.out.println("Smaller number: " + Math.min(a, b));
}
}
Save the Java program as LargerMath.java.
Variation: difference between the two numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
large = max(a, b)
small = min(a, b)
print("Difference:", large - small)
Enter first number: 4
Enter second number: 12
Difference: 8
Variation: decimal numbers
Replace int() with float(). 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: "))
print("Larger number:", max(a, b))
print("Smaller number:", min(a, b))
Enter first number: 2.5
Enter second number: -1.5
Larger number: 2.5
Smaller number: -1.5
Common Mistakes
- Forgetting int().
input()returns text, and text is compared letter by letter. Then “9” looks bigger than “10”, because “9” comes after “1”. - Mixing the signs. The larger number uses
>and the smaller number uses<. Copying one block and forgetting to flip the sign gives the wrong answer. - Assuming the numbers are different. Test the program with two equal numbers. Method 2 handles it, but a version that prints only when
a > borb > awould print nothing. - Starting large at 0. If both numbers are negative,
large = 0would wrongly stay 0. Always start from one of the real numbers. - Using = instead of ==. In C, Java and Python, a single
=assigns a value. Use==to test equality. - Wrong Java file name. A public class must be saved in a file with the same name, such as
LargerOfTwo.java.
Practice Questions
- Print “Both numbers are equal” when a and b are the same, and otherwise print the larger number.
- Swap the values of two variables so that a always holds the smaller number.
- Read two numbers and print them in ascending order.
- Read the marks of two students and print the name of the student who scored higher.
- Extend the program to three numbers. Hint: keep the same idea and add one more comparison for each box.
- Write the program without using
if,max()ormin(). Hint: Method 7.
Frequently Asked Questions
How do you find the larger of two numbers in Python?
Read both numbers with int(input()) and test if a > b. If the test is true, a is larger, otherwise b is larger. The built-in max(a, b) does the same in one step.
How do you find the smaller of two numbers?
Use the same test with the other sign: if a < b means a is smaller. In Python, min(a, b) gives the smaller number directly.
What happens if both numbers are equal?
Neither a > b nor a < b is true, so the running-variable method keeps the first number for both answers. Since both numbers are the same, the output is still correct. You can add an elif or else branch to print a message for equal numbers.
How many comparisons are needed?
One comparison is enough to decide which number is larger and which is smaller (Method 2). The running-variable method uses two, one for the larger and one for the smaller, but it is easier to extend to more numbers.
Does the program work for negative numbers?
Yes. Comparison operators work on negative values too, and -2 is greater than -5.
Why does my program say 9 is larger 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 larger number without an if statement?
Yes. Use max(a, b), sort a list, or use the formula (a + b + abs(a - b)) // 2.
What is the ternary operator?
It is a short form of if-else that gives a value. In Python it is x if condition else y, and in C and Java it is condition ? x : y.
What is the difference between max() and writing the if statement myself?
Both give the same result. The if statement shows your understanding of comparison and is what most practical exams expect, while max() is shorter and useful in real programs.
How do I extend this to three numbers or a list?
Keep the same idea. Start large and small with the first value, and use one if for each remaining value. For a list, put those ifs inside a loop, or use max(list) and min(list).