Java – Find Largest of Three Numbers
To find the largest of three numbers in Java, compare the three values and store the greatest value in a variable. This can be done using nested if-else statements, an if-else-if ladder, a ternary operator, or Java’s Math.max() method.
For example, if the three numbers are 20, 10, and 30, the largest number is 30.
In this tutorial, we shall learn how to find the largest of three numbers using different approaches.
You can find largest of three numbers using if-else statement, if-else-if ladder or ternary operator. We shall go through each of these with example programs.
Example 1 – Find Largest of Three Numbers using If-Else
In this example, we shall use the following algorithm to find the largest of three numbers. We shall realize this algorithm using Java If-Else statement.
Algorithm to Find the Largest of Three Numbers with Nested If-Else
- Start.
- Take three numbers in a, b, c.
- Check if a is greater than b.
- If above condition is true, go to step 5, else go to step 7.
- Check if a is greater than c.
- If above condition is true, a is the largest, else c is the largest. Go to step 9.
- Check if b is greater than c.
- If above condition is true, b is the largest, else c is the largest.
- Stop.
Java Program
/**
* Java Program - Find Largest of Three Numbers
*/
public class LargestInThree {
public static void main(String[] args) {
//three numbers
int a = 20;
int b = 10;
int c = 30;
int largest;
//find the largest
if(a>b) {
if(a>c) {
largest = a;
}
} else {
if(b>c) {
largest = b;
} else {
largest = c;
}
}
System.out.println(largest + " is the largest.");
}
}
Output
Run the above program and we shall get the largest of three numbers as shown in the following console output.
30 is the largest.
Correct Nested If-Else for All Three Number Orders
The nested comparison needs an else branch when a > b but a is not greater than c. Without that branch, largest is not assigned on every possible execution path. The complete nested if-else form is shown below.
public class LargestInThreeNestedIf {
public static void main(String[] args) {
int a = 20;
int b = 10;
int c = 30;
int largest;
if (a > b) {
if (a > c) {
largest = a;
} else {
largest = c;
}
} else {
if (b > c) {
largest = b;
} else {
largest = c;
}
}
System.out.println(largest + " is the largest.");
}
}
30 is the largest.
Example 2 – Find Largest of Three Numbers using If-Else-If Ladder
In this example, we shall use the following algorithm to find the largest of three numbers. We shall implement the following algorithm using Java if-else-if statement.
Algorithm to Compare Three Numbers with If-Else-If
- Start.
- Take three numbers in a, b, c.
- Check if a is greater than b and a is greater than c.
- If above condition is true, a is largest and go to step 7, else go to step 5.
- Check if b is greater than c.
- If above condition is true, b is the largest, else c is the largest.
- Stop.
Java Program
/**
* Java Program - Find Largest of Three Numbers
*/
public class LargestInThree {
public static void main(String[] args) {
//three numbers
int a = 20;
int b = 10;
int c = 30;
int largest;
//find the largest
if(a>b && a>c) {
largest = a;
} else if (b>c) {
largest = b;
} else {
largest = c;
}
System.out.print(largest + " is the largest.");
}
}
Output
Run the above program and we shall get that 30 is the largest of the three numbers.
30 is the largest.
How the If-Else-If Comparison Selects the Largest Number
The first condition, a > b && a > c, checks whether a is greater than both other values. If this condition is false, the program compares b with c. If b > c, then b is selected; otherwise c is selected.
With a = 20, b = 10, and c = 30, the first condition is false because 20 > 30 is false. The second condition, 10 > 30, is also false, so the final else assigns 30 to largest.
Find Largest of Three Numbers in Java Using Scanner
If the three numbers should be entered by the user instead of being written directly in the program, use the Scanner class. After reading the values, the same comparison logic can be applied.
import java.util.Scanner;
public class LargestOfThreeScanner {
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 largest;
if (a >= b && a >= c) {
largest = a;
} else if (b >= a && b >= c) {
largest = b;
} else {
largest = c;
}
System.out.println(largest + " is the largest.");
scanner.close();
}
}
For example, entering 45, 72, and 18 produces the following result.
Enter first number: 45
Enter second number: 72
Enter third number: 18
72 is the largest.
Example – Find Largest 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 Largest of Three Numbers
*/
public class LargestInThree {
public static void main(String[] args) {
//three numbers
int a = 20;
int b = 10;
int c = 30;
int largest = (a>b)?(a>c?a\:c):(b>c?b\:c);
System.out.print(largest + " is the largest.");
}
}
Output
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.
30 is the largest.
Ternary Operator Logic for the Largest of Three Values
A nested ternary expression performs the same comparisons in expression form. Conceptually, it first asks whether a is greater than b. Depending on that result, it compares either a with c or b with c.
For readability, an if-else-if ladder is often easier to follow when the comparison logic becomes more complex.
Find the Largest of Three Numbers with Math.max() in Java
Java also provides Math.max(), which returns the greater of two values. To compare three numbers, call it twice: first compare two values, and then compare that result with the third value.
int largest = Math.max(a, Math.max(b, c));
The following complete program uses this approach.
public class LargestOfThreeMathMax {
public static void main(String[] args) {
int a = 20;
int b = 10;
int c = 30;
int largest = Math.max(a, Math.max(b, c));
System.out.println(largest + " is the largest.");
}
}
30 is the largest.
Largest of Three Numbers When Two or More Values Are Equal
Equal values require a small distinction between finding the largest value and identifying a unique largest number. If the goal is only to find the maximum value, comparisons using >= work naturally.
For example, if the values are 25, 25, and 10, the largest value is still 25. If all three values are 7, the largest value is 7, although no single input is uniquely greater than the others.
public class LargestWithEqualValues {
public static void main(String[] args) {
int a = 25;
int b = 25;
int c = 10;
int largest;
if (a >= b && a >= c) {
largest = a;
} else if (b >= a && b >= c) {
largest = b;
} else {
largest = c;
}
System.out.println(largest + " is the largest value.");
}
}
25 is the largest value.
Which Java Approach Should You Use for Three-Number Comparison?
- Nested if-else: useful for learning how one comparison can lead to another comparison.
- If-else-if ladder: keeps the decision logic direct and readable for three explicit values.
- Scanner with if-else-if: suitable when the three numbers are supplied at runtime.
- Ternary operator: provides a compact expression, but nested ternary expressions can be harder to read.
- Math.max(): is concise when only the maximum value is required.
Java Largest-of-Three Number Comparison Checks
When reviewing a largest-of-three-numbers program, check the comparison logic with more than one ordering of the inputs. Useful cases include:
- the first value is largest, such as
30, 20, 10; - the second value is largest, such as
10, 30, 20; - the third value is largest, such as
10, 20, 30; - two values are equal and largest, such as
30, 30, 10; - all three values are equal, such as
20, 20, 20; - the inputs include negative values, such as
-4, -9, -2.
Testing these cases helps verify that every branch assigns the correct largest value rather than working only for one particular ordering of the numbers.
Summary of Finding the Largest of Three Numbers in Java
To find the largest of three numbers in Java, compare each candidate with the other values and retain the maximum. An if-else-if ladder is a clear choice for learning conditional comparisons, Scanner can be added for runtime input, a ternary expression provides a compact alternative, and Math.max() provides the shortest direct maximum calculation.
In this Java Tutorial, we learned how to find the largest of three given numbers, using different conditional expressions in Java programming language.
TutorialKart.com