0
0
JavaProgramBeginner · 2 min read

Java Program to Find Largest of Three Numbers

To find the largest of three numbers in Java, use if-else statements to compare the numbers and print the largest, like if (a >= b && a >= c) System.out.println(a); else if (b >= a && b >= c) System.out.println(b); else System.out.println(c);.
📋

Examples

Inputa=5, b=10, c=3
OutputLargest number is 10
Inputa=7, b=7, c=7
OutputLargest number is 7
Inputa=-1, b=-5, c=-3
OutputLargest number is -1
🧠

How to Think About It

To find the largest of three numbers, compare the first number with the other two using >=. If it is greater or equal to both, it is the largest. Otherwise, compare the second number with the third. The one that is greater or equal is the largest.
📐

Algorithm

1
Get three numbers as input.
2
Check if the first number is greater than or equal to both the second and third numbers.
3
If yes, the first number is the largest.
4
Otherwise, check if the second number is greater than or equal to the third number.
5
If yes, the second number is the largest.
6
Otherwise, the third number is the largest.
7
Print the largest number.
💻

Code

java
public class LargestOfThree {
    public static void main(String[] args) {
        int a = 5, b = 10, c = 3;
        int largest;
        if (a >= b && a >= c) {
            largest = a;
        } else if (b >= a && b >= c) {
            largest = b;
        } else {
            largest = c;
        }
        System.out.println("Largest number is " + largest);
    }
}
Output
Largest number is 10
🔍

Dry Run

Let's trace the example where a=5, b=10, c=3 through the code.

1

Compare a with b and c

Check if 5 >= 10 and 5 >= 3. This is false because 5 is not >= 10.

2

Compare b with c

Check if 10 >= 5 and 10 >= 3. This is true because 10 >= 5 and 10 >= 3.

3

Assign largest

Set largest = 10.

4

Print result

Output: Largest number is 10.

StepConditionResultLargest
1a >= b && a >= c (5 >= 10 && 5 >= 3)falseundefined
2b >= a && b >= c (10 >= 5 && 10 >= 3)true10
3Assign largest = b-10
4Print largest-10
💡

Why This Works

Step 1: Compare first number

We use if (a >= b && a >= c) to check if the first number is the largest by comparing it to the other two.

Step 2: Compare second number

If the first number is not largest, we check if the second number is larger or equal to the third using else if (b >= a && b >= c).

Step 3: Assign and print largest

Based on the comparisons, we assign the largest number to a variable and print it with System.out.println.

🔄

Alternative Approaches

Using Math.max
java
public class LargestOfThree {
    public static void main(String[] args) {
        int a = 5, b = 10, c = 3;
        int largest = Math.max(a, Math.max(b, c));
        System.out.println("Largest number is " + largest);
    }
}
This method is shorter and uses built-in functions but may be less clear for beginners learning comparisons.
Using nested if-else
java
public class LargestOfThree {
    public static void main(String[] args) {
        int a = 5, b = 10, c = 3;
        int largest = a;
        if (b > largest) {
            largest = b;
        }
        if (c > largest) {
            largest = c;
        }
        System.out.println("Largest number is " + largest);
    }
}
This approach updates the largest number step-by-step and is easy to understand.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of comparisons regardless of input size, so it runs in constant time O(1).

Space Complexity

Only a few variables are used to store input and the largest number, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time, but using Math.max is concise and readable, while if-else gives more control.

ApproachTimeSpaceBest For
If-else comparisonsO(1)O(1)Clear logic and beginner understanding
Math.max methodO(1)O(1)Concise and clean code
Nested if updatesO(1)O(1)Stepwise comparison and easy to extend
💡
Use Math.max for a concise way to find the largest number among multiple values.
⚠️
Beginners often forget to use >= instead of >, which can cause incorrect results when numbers are equal.