Java program to find largest element of an array

h‮/:sptt‬/www.theitroad.com

To find the largest element of an array in Java, you can iterate over the array and keep track of the largest element seen so far. Here's an example program that demonstrates this:

public class LargestElement {
    public static void main(String[] args) {
        int[] arr = { 10, 20, 30, 40, 50 };
        int largest = arr[0];

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > largest) {
                largest = arr[i];
            }
        }

        System.out.println("Largest element of the array: " + largest);
    }
}

In this program, we define an integer array arr and initialize it with some values. We then define a variable largest and initialize it with the first element of the array. We then iterate over the remaining elements of the array and compare each element with the current value of largest. If an element is greater than largest, we update the value of largest to that element.

After iterating over all the elements of the array, we print out the largest element to the console using the System.out.println method.

When you run the program, the output will be:

Largest element of the array: 50

As you can see, the program successfully finds the largest element of the array and prints it to the console.