Write a Java Program to Find Largest Element of an Array

In Java, an array is a data structure that stores a fixed-size sequence of elements of the same data type.

It’s a common task to find the largest element in an array, which can be done using a simple algorithm.

Here’s how to write a Java program to find the largest element of an array.


First, create an array of integers:

int[] numbers = {3, 7, 2, 10, 4, 5};

Next, create a variable to hold the largest number:

int largest = numbers[0]

This initializes the largest variable to the first element of the array.

Now, loop through the array and compare each element to the current largest:

cssCopy codefor (int i = 1; i < numbers.length; i++) {
    if (numbers[i] > largest) {
        largest = numbers[i];
    }
}

This loop starts at the second element of the array and compares each element to the current largest.

If the element is larger than the current largest, it becomes the new largest.

Finally, print out the largest number:

System.out.println("The largest number is " + largest);

Here’s the full program:

public class LargestElement {
    public static void main(String[] args) {
        int[] numbers = {3, 7, 2, 10, 4, 5};
        int largest = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > largest) {
                largest = numbers[i];
            }
        }
        System.out.println("The largest number is " + largest);
    }
}

When you run this program, it will output:

The largest number is 10

That’s it! This program shows how to find the largest element of an array in Java using a simple algorithm.

You can also make this program more dynamic by allowing the user to enter their own array of integers using the Scanner class.

Here’s an updated version of the program:

import java.util.Scanner;

public class LargestElement {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();
        int[] numbers = new int[size];
        for (int i = 0; i < size; i++) {
            System.out.print("Enter element " + (i+1) + ": ");
            numbers[i] = scanner.nextInt();
        }
        int largest = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] > largest) {
                largest = numbers[i];
            }
        }
        System.out.println("The largest number is " + largest);
    }
}

This version of the program prompts the user to enter the size of the array, creates an array of that size, and then prompts the user to enter each element of the array.

Then it finds the largest element and prints it out.

This version of the program is more flexible, as it allows the user to enter any array of integers, not just a fixed array defined in the program.


In conclusion, finding the largest element of an array in Java is a common programming task that can be accomplished using a simple algorithm.

By using the for loop to compare each element of the array to the current largest, you can find the largest element in the array.

The program can be made more flexible by allowing the user to enter their own array of integers using the Scanner class.