Skip to main content

Command Palette

Search for a command to run...

Java Program to Find Largest Number in an Array

Updated
โ€ข2 min read

Finding the largest number in an array is a common Java interview question that helps test understanding of arrays, loops, and logical thinking.


๐Ÿ”น Approach

  1. Assume the first element as the largest number.

  2. Traverse the array using a loop.

  3. Compare each element with the current largest value.

  4. 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.