Java – Find Smallest of Three Numbers
To find the smallest of three numbers in Java, compare the values and keep the minimum. You can do this with nested if-else statements, an if-else-if ladder, a ternary expression, or Math.min().
For example, if a = 20, b = 10, and c = 30, the smallest value is 10. The examples below use the same values so that the comparison logic is easy to follow.
Example 1 – Find Smallest of Three Numbers using If-Else
In this example, we shall use the following algorithm to find the smallest of three numbers. We shall realize this algorithm using Java If-Else statement.
If-Else Algorithm for Comparing Three Numbers
- Start.
- Take three numbers in a, b, c.
- Check if a is less than b.
- If above condition is true, go to step 5, else go to step 7.
- Check if c is less than a.
- If above condition is true, c is the smallest, else a is the smallest. Go to step 9.
- Check if b is less than c.
- If above condition is true, b is the smallest, else c is the smallest.
- Stop.
Java Program
/**
* Java Program - Find Smallest of Three Numbers
*/
public class SmallestInThree {
public static void main(String[] args) {
//three numbers
int a = 20;
int b = 10;
int c = 30;
int smallest;
//find the smallest
if(a<b) {
if(c<a) {
smallest = c;
} else {
smallest = a;
}
} else {
if(b<c) {
smallest = b;
} else {
smallest = c;
}
}
System.out.println(smallest + " is the smallest.");
}
}
Run the above program and we shall get the smallest of three numbers as shown in the following console output.
10 is the smallest.
The outer condition first decides whether the minimum must come from the pair a and c, or from the pair b and c. The inner if then completes that comparison.
Example 2 – Find Smallest of Three Numbers using If-Else-If Ladder
In this example, we shall use the following algorithm to find the smallest of three numbers. We shall implement the following algorithm using Java if-else-if statement.
If-Else-If Conditions for the Minimum Value
- Start.
- Take three numbers in a, b, c.
- Check if a is less than b and a is less than c.
- If above condition is true, a is smallest and go to step 7, else go to step 5.
- Check if b is less than c.
- If above condition is true, b is the smallest, else c is the smallest.
- Stop.
Java Program
/**
* Java Program - Find Smallest of Three Numbers
*/
public class SmallestInThree {
public static void main(String[] args) {
//three numbers
int a = 20;
int b = 10;
int c = 30;
int smallest;
if(a<b && a<c) {
smallest = a;
} else if (b<c) {
smallest = b;
} else {
smallest = c;
}
System.out.println(smallest + " is the smallest.");
}
}
Run the above program and we shall get that 10 is the smallest of the three numbers.
10 is the smallest.
The first condition uses the logical AND operator because a must be smaller than both of the other values. If that test fails, comparing b with c is enough to determine the result.
Example 3 – Find Smallest of Three Numbers using Ternary Operator
The algorithm for this example, is kind of same as that of the first example, where we used if-else statement. But, in this example, we use nested ternary operator to crunch all the code to a single line.
Java Program
/**
* Java Program - Find Smallest of Three Numbers
*/
public class SmallestInThree {
public static void main(String[] args) {
//three numbers
int a = 20;
int b = 10;
int c = 30;
int smallest = (a<b)?(a<c?a:c):(b<c?b:c);
System.out.println(smallest + " is the smallest.");
}
}
Run the above program and you shall get the following output. You may change the values for a, b and c and check the result.
10 is the smallest.
A nested ternary expression is compact, but it is harder to scan than a short if-else-if ladder. Use it when the expression remains clear to the reader.
Find the Smallest of Three Values with Math.min()
Java’s Math.min() method compares two values at a time. To compare three numbers, call it twice by nesting one call inside the other.
int smallest = Math.min(a, Math.min(b, c));
The inner call returns the smaller of b and c. The outer call then compares that result with a.
public class SmallestUsingMathMin {
public static void main(String[] args) {
int a = 20;
int b = 10;
int c = 30;
int smallest = Math.min(a, Math.min(b, c));
System.out.println(smallest + " is the smallest.");
}
}
10 is the smallest.
Read Three Numbers from User Input and Print the Smallest
When the values are not known in advance, read them with Scanner and apply the same minimum comparison. The following program accepts three integers from standard input.
import java.util.Scanner;
public class SmallestFromInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = scanner.nextInt();
System.out.print("Enter second number: ");
int b = scanner.nextInt();
System.out.print("Enter third number: ");
int c = scanner.nextInt();
int smallest = Math.min(a, Math.min(b, c));
System.out.println(smallest + " is the smallest.");
scanner.close();
}
}
For input values 12, -4, and 7, the result is:
-4 is the smallest.
What Happens When Two or Three Numbers Are Equal?
These approaches still return a correct minimum when values are equal. For example, if the values are 5, 5, and 9, the minimum is 5. If all three values are 7, the minimum is 7.
The programs in this tutorial print the minimum value, not which variable supplied it. If you need to report ties such as “a and b are both smallest,” add equality checks separately.
Test Cases for a Java Smallest-of-Three Program
A minimum-of-three program should be checked with more than one ordering of the inputs. Include cases where each variable is the minimum, along with negative values and equal values.
a | b | c | Expected smallest value |
|---|---|---|---|
| 2 | 8 | 5 | 2 |
| 9 | 3 | 6 | 3 |
| 12 | 18 | 4 | 4 |
| -2 | 7 | 0 | -2 |
| 5 | 5 | 9 | 5 |
| 7 | 7 | 7 | 7 |
Choosing a Java Approach for the Smallest of Three Numbers
- Use nested
if-elsewhen you want to practice step-by-step branching. - Use an
if-else-ifladder when you want explicit and readable comparison conditions. - Use a nested ternary operator for a compact expression when readability remains acceptable.
- Use
Math.min()when you only need the minimum value and do not need custom branching.
All four approaches produce the same minimum value for the same three numeric inputs. In this Java Tutorial, we learned how to find the smallest of three given numbers using conditional statements, a ternary expression, and Math.min().
TutorialKart.com