Google News
logo
Java Program to Find Largest Element of an Array
In the following example of Java program to find the largest element of an array :
Program :
public class LargestElement {
    public static void main(String[] args) {
        int[] arr = {2, 3, 5, 7, 1, 9, 4};
        int max = arr[0];
        
        for(int i=1; i<arr.length; i++){
            if(arr[i] > max){
                max = arr[i];
            }
        }
        
        System.out.println("Largest element of the array: " + max);
    }
}
Output :
Largest element of the array: 9
In this program, we have an integer array arr. We initialize a variable max with the first element of the array. Then, we loop through the rest of the array and compare each element with max.

If the current element is greater than max, we update the value of max. Finally, we print the value of max, which is the largest element of the array.