Introduction
Finding the largest number is one of the most common beginner-level programming exercises in Java. The program compares two or more numbers and determines which one has the greatest value.
For example:
Input:
25, 40
Output:
Largest Number = 40
Another example:
Input:
15, 45, 30
Output:
Largest Number = 45
The Java Largest Number program is frequently asked in coding interviews because it helps beginners understand comparison operators, conditional statements (if-else), loops, arrays, and user input.
In this tutorial, you’ll learn different ways to find the largest number in Java, including comparing two numbers, three numbers, array elements, and user input.
What is the Largest Number?
The largest number is the number that has the greatest value among a given set of numbers.
Example:
Numbers:
10, 25, 18
Largest Number:
25
Another example:
Numbers:
150, 75, 300, 90
Largest Number:
Java Program to Find the Largest of Two Numbers
public class LargestNumber {
public static void main(String[] args) {
int a = 25;
int b = 40;
if (a > b) {
System.out.println("Largest Number = " + a);
} else {
System.out.println("Largest Number = " + b);
}
}
}
Output
How the Program Works?
Step 1: Declare Variables
int a = 25;
int b = 40;
These variables store the numbers to compare.
Step 2: Compare the Numbers
if(a > b)
If a is greater than b, then a is the largest number.
Otherwise, b is the largest.
Step 3: Display the Result
System.out.println("Largest Number = " + b);
The program prints the larger value.
Java Program to Find the Largest of Three Numbers
public class LargestNumber {
public static void main(String[] args) {
int a = 35;
int b = 60;
int c = 45;
int largest = a;
if (b > largest) {
largest = b;
}
if (c > largest) {
largest = c;
}
System.out.println("Largest Number = " + largest);
}
}
Output
Java Program Using Nested if-else
public class LargestNumber {
public static void main(String[] args) {
int a = 12;
int b = 30;
int c = 18;
if (a >= b && a >= c) {
System.out.println(a + " is the largest.");
} else if (b >= a && b >= c) {
System.out.println(b + " is the largest.");
} else {
System.out.println(c + " is the largest.");
}
}
}
Output
Java Program to Find the Largest Number in an Array
public class LargestNumber {
public static void main(String[] args) {
int[] numbers = {15, 85, 40, 65, 22};
int largest = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
System.out.println("Largest Number = " + largest);
}
}
Output
Java Program Using User Input
import java.util.Scanner;
public class LargestNumber {
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();
if (a > b) {
System.out.println("Largest Number = " + a);
} else {
System.out.println("Largest Number = " + b);
}
scanner.close();
}
}
Output
Enter second number: 75
Largest Number = 120
Algorithm
Finding the Largest of Two Numbers
- Read two numbers.
- Compare the first number with the second.
- If the first number is greater, display it.
- Otherwise, display the second number.
Finding the Largest in an Array
- Store the first element as the largest.
- Traverse the array.
- Compare each element with the current largest value.
- Update the largest value if a bigger number is found.
- Print the largest number.
Flowchart
Start
│
Read Numbers
│
Compare Values
│
Update Largest
│
Display Largest Number
│
End
Time Complexity
| Program | Time Complexity |
|---|---|
| Two Numbers | O(1) |
| Three Numbers | O(1) |
| Array | O(n) |
where n is the number of elements in the array.
Space Complexity
| Program | Space Complexity |
|---|---|
| Two Numbers | O(1) |
| Three Numbers | O(1) |
| Array | O(1) |
Common Mistakes
1. Using Assignment Instead of Comparison
Incorrect:
if(a = b)
Correct:
if(a == b)
2. Initializing Largest Incorrectly
Incorrect:
int largest = 0;
If all numbers are negative, the result will be incorrect.
Correct:
int largest = numbers[0];
3. Forgetting to Update the Largest Value
Incorrect:
if(numbers[i] > largest) {
}
Correct:
if(numbers[i] > largest) {
largest = numbers[i];
}
4. Incorrect Loop Starting Index
Incorrect:
for(int i = 0; i < numbers.length; i++)
Although it works, comparing the first element with itself is unnecessary.
Better:
for(int i = 1; i < numbers.length; i++)
5. Forgetting to Close Scanner
Always close the Scanner object.
scanner.close();
Real-Life Applications
Finding the largest number is used in many practical applications, including:
- Finding the highest student marks
- Identifying the maximum salary
- Determining the highest temperature
- Data analysis
- Sorting algorithms
- Competitive programming
- Statistical calculations
Summary
The Java Largest Number program is one of the most important beginner-level coding exercises. It teaches essential programming concepts such as comparison operators, conditional statements, loops, arrays, and user input handling.
For comparing two numbers, an if-else statement or Math.max() is simple and efficient. For multiple numbers or arrays, iterating through the values while keeping track of the current largest element is the standard and most efficient approach.