Java Program to Find Largest Number in an Array
Finding the largest number in an array is a common Java interview question that helps test understanding of arrays, loops, and logical thinking.
๐น Approach
Assume the first element as the largest number.
Traverse the array using a loop.
Compare each element with the current largest value.
Update the largest value when a bigger number is found.
๐น Java Program
public class LargestNumber {
public static void main(String[] args) {
int[] numbers = {12, 45, 7, 89, 34, 67};
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
Largest Number = 89
๐น Using Enhanced For Loop (Alternative)
int[] numbers = {12, 45, 7, 89, 34, 67};
int largest = numbers[0];
for(int num : numbers) {
if(num > largest) {
largest = num;
}
}
System.out.println("Largest Number = " + largest);
โ Conclusion
This program demonstrates how to iterate through arrays and compare values efficiently. It is one of the most frequently asked logical questions in Java interviews.
๐ฅ Promotional Content
Improve your Java coding skills, problem-solving techniques, and real-time development knowledge with the Best Java Real Time Projects Online Training in 2026 by ashok it.