# Program to print the largest element in an array

In this program, we need to find out the largest element present in the array and display it. This can be accomplished by looping through the array from start to end by comparing max with all the elements of an array. If any of element is greater than max, then store a value of the element in max. Initially, max will hold the value of the first element. At the end of the loop, max represents the largest element in the array.

In the above array, initially, max will hold the value 25. In the 1st iteration, max will be compared with 11, since 11 is less than max. Max will retain its value. In the next iteration, it will be compared to 7, 7 is also less than max, no change will be made to the max. Now, max will be compared to 75. 75 is greater than max so that max will hold the value of 75. Continue this process until the end of the array is reached. At the end of the loop, max will hold the largest element in the array.

### Algorithm

1. STEP 1: START
2. STEP 2: INITIALIZE arr[] = {25, 11, 7, 75, 56}
3. STEP 3: max = arr
4. STEP 4: REPEAT STEP 5 for(i=0; i< arr.length; i++)
5. STEP 5: if(arr[i]>max) max=arr[i]
6. STEP 6: PRINT “Largest element in given array:”
7. STEP 7: PRINT max
8. STEP 8: END

### Program:

1. public class LargestElement_array {
2.     public static void main(String[] args) {
3.
4.         //Initialize array
5.         int [] arr = new int [] {25, 11, 7, 75, 56};
6.         //Initialize max with first element of array.
7.         int max = arr;
8.         //Loop through the array
9.         for (int i = 0; i < arr.length; i++) {
10.             //Compare elements of array with max
11.            if(arr[i] > max)
12.                max = arr[i];
13.         }
14.         System.out.println(“Largest element present in given array: ” + max);
15.     }
16. }

Output:

`Largest element present in given array: 75`