In this tutorial, we will learn how to write a Java program to find the largest of three numbers. We will use the if-else statement to compare three numbers and determine which one has the greatest value.
We will also explore how to find the largest number using the Scanner class to accept user input and the built-in Math.max() method. Each approach includes a complete Java program, sample output, and a step-by-step explanation.
Java Program to Find the Largest of Three Numbers Using If-Else
The simplest way to find the largest of three numbers in Java is to compare them using an if-else-if ladder.
Problem Statement
Write a Java program that stores three integer numbers in variables, compares their values, and displays the largest number on the console. For example, if the three numbers are 25, 60, and 40, the program should display 60 as the largest number.
Complete Java Program
public class LargestOfThreeNumbers {
public static void main(String[] args) {
// Declare and initialize the three numbers to be compared
int num1 = 25;
int num2 = 60;
int num3 = 40;
// Variable to store the largest number found
int largest;
// Check if num1 is greater than or equal to both num2 and num3
if (num1 >= num2 && num1 >= num3) {
largest = num1;
}
// If num1 is not the largest, check if num2 is greater than or equal to both num1 and num3
else if (num2 >= num1 && num2 >= num3) {
largest = num2;
}
// If neither num1 nor num2 is the largest, then num3 must be the largest
else {
largest = num3;
}
// Display the input numbers and the final result to the console
System.out.println("First Number : " + num1);
System.out.println("Second Number : " + num2);
System.out.println("Third Number : " + num3);
System.out.println("Largest Number: " + largest);
}
}Expected Output:
First Number : 25 Second Number : 60 Third Number : 40 Largest Number: 60
In this example program:
- We declare three integer variables named num1, num2, and num3 and assign them the values 25, 60, and 40, respectively.
- Then, we declare a variable named largest to store the largest number. The largest variable will store the greatest value among the three numbers.
- After that, we compare the numbers using the if-else-if ladder statement.
- The first condition checks whether num1 is greater than or equal to both num2 and num3. If the condition is true, num1 is the largest.
- If the first condition is false, the else if condition checks whether num2 is greater than or equal to both other numbers. If it is, num2 becomes the largest.
- If neither condition is true, num3 is the largest.
- The && operator means logical AND. Both comparisons connected by && must be true for the condition to succeed.
- We use the greater than or equal to operator (>=) instead of the greater than operator (>) so the program also handles cases in which two or all three numbers are equal.
- The System.out.println() statement displays the largest number on the console.
Java Program to Find the Largest of Three Numbers Using Scanner
In the previous example program, we assigned values directly to the variables, num1, num2, and num3.
Problem Statement
Now, let us write a Java program that accepts three numbers from the user’s keyword at runtime. Java provides the Scanner class in the java.util package to read input from the keyboard.
Complete Java Program
// Import the Scanner class to read input from the user
import java.util.Scanner;
public class LargestOfThreeScanner {
public static void main(String[] args) {
// Create a Scanner object to take input from the keyboard
Scanner scanner = new Scanner(System.in);
// Prompt the user and read the first number
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
// Prompt the user and read the second number
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
// Prompt the user and read the third number
System.out.print("Enter the third number: ");
int num3 = scanner.nextInt();
// Variable to store the largest number found
int largest;
// Check if num1 is greater than or equal to both num2 and num3
if (num1 >= num2 && num1 >= num3) {
largest = num1;
}
// If num1 is not the largest, check if num2 is greater than or equal to both num1 and num3
else if (num2 >= num1 && num2 >= num3) {
largest = num2;
}
// If neither num1 nor num2 is the largest, then num3 must be the largest
else {
largest = num3;
}
// Display the final result to the user
System.out.println("Largest number = " + largest);
// Close the scanner to free up system resources
scanner.close();
}
}Expected Output:
Enter the first number: 45 Enter the second number: 78 Enter the third number: 32 Largest number = 78
In this example program, you can change the input values each time you run the program without modifying the source code.
Java Program to Find the Largest of Three Numbers Using Math.max()
Java provides the built-in Math.max() method to return the greater of two numbers. By using this method twice, we can find the largest of three numbers in Java.
Problem Statement
Write a Java program to find the largest of three numbers using the Math.max() method. The program should compare three integer numbers and display the greatest value as the output.
Complete Java Program
public class LargestUsingMathMax {
public static void main(String[] args) {
// Declare and initialize three integer numbers
int num1 = 35;
int num2 = 85;
int num3 = 55;
// Find the largest number using nested Math.max() functions.
// Math.max(num2, num3) finds the maximum between the last two numbers,
// and the outer Math.max() compares num1 with that result.
int largest = Math.max(num1, Math.max(num2, num3));
// Display the input values and the final largest number to the console
System.out.println("First Number : " + num1);
System.out.println("Second Number : " + num2);
System.out.println("Third Number : " + num3);
System.out.println("Largest Number: " + largest);
}
}Expected Output:
First Number : 35 Second Number : 85 Third Number : 55 Largest Number: 85
In this example program, the statement int largest = Math.max(num1, Math.max(num2, num3)); first compares num2 and num3 and returns the greater value. The outer Math.max() then compares that result with num1.
For the given example:
- Math.max(85, 55) returns 85.
- Math.max(35, 85) returns 85.
Therefore, the largest number is 85.
This approach is concise and useful when you only need to determine the maximum value.
Find the Greatest of Three Numbers Using the Ternary Operator in Java
The conditional or ternary operator (?:) provides a simple way to find the largest of three numbers in Java. It allows us to evaluate a condition and select one of two possible results in a single expression.
Problem Statement
Write a Java program to find the greatest of three numbers using the ternary operator. The program should compare three integer numbers and display the greatest value as the output.
Complete Java Program
public class LargestUsingTernary {
public static void main(String[] args) {
// Declare and initialize three integer numbers
int num1 = 20;
int num2 = 50;
int num3 = 30;
// Find the largest number using a nested ternary operator (condition ? if_true : if_false)
// 1. Check if num1 is greater than or equal to both num2 and num3. If true, assign num1.
// 2. If false, check the inner condition: if num2 >= num3, assign num2; otherwise, assign num3.
int largest = (num1 >= num2 && num1 >= num3)
? num1
: (num2 >= num3 ? num2 : num3);
// Display the final result to the console
System.out.println("Largest number = " + largest);
}
}Expected Output:
Largest number = 50
In this example program, the first condition checks whether num1 is greater than or equal to both other numbers. If it evaluates to true, num1 is selected.
Otherwise, the second ternary expression compares num2 and num3 and selects the greater value.
Although the ternary operator makes the code shorter, the if-else-if approach is often easier for beginners to understand and debug.
Java Program to Find the Largest of Three Decimal Numbers
In all the previous examples, we have taken integer values to find the largest of three numbers. If the numbers contain decimal values, use the double data type instead of int.
Problem Statement
Write a Java program to find the largest of three decimal numbers using the double data type. The program should compare the three decimal values and display the greatest number as the output.
Program Code
public class LargestOfDecimalNumbers {
public static void main(String[] args) {
// Declare and initialize three decimal (double) numbers
double num1 = 12.5;
double num2 = 18.75;
double num3 = 15.25;
// Variable to store the largest decimal number found
double largest;
// Check if num1 is greater than or equal to both num2 and num3
if (num1 >= num2 && num1 >= num3) {
largest = num1;
}
// If num1 is not the largest, check if num2 is greater than or equal to both num1 and num3
else if (num2 >= num1 && num2 >= num3) {
largest = num2;
}
// If neither num1 nor num2 is the largest, then num3 must be the largest
else {
largest = num3;
}
// Display the final result to the console
System.out.println("Largest number = " + largest);
}
}Expected Output:
Largest number = 18.75
The comparison logic is the same as in the integer in the previous examples. The difference is that the variables use double, which supports floating-point values.
Which Approach Is the Best Approach?
- If-else-if ladder: Best for beginners learning conditional statements and comparison operators.
- Scanner with if-else: Best when the program needs to accept user input at runtime.
- Math.max(): Best when you want a concise way to calculate the maximum value.
- Ternary operator: Useful for practicing conditional expressions, although nested expressions using the ternary operator can be harder to read.
For beginners, I recommend starting with the if-else-if approach because it clearly demonstrates how Java compares values and makes decisions.



