Home >>Java Programs >Java Program to print the largest element in an array
In this example, we will create a java program to find out the largest element present in the array and display it. This can be done by looping through the array from start to end by comparing the max with all the elements of an array. If any of element is greater than the max then store the value of the element in max. Initially, max will hold the value of the first element. At the end of the loop, max will represent the largest element present in the array.
Algorithm:
public class Main
{
public static void main(String[] args)
{
int [] arr = new int [] {25, 11, 17, 57, 62, 91, 31, 29, 73};
int max = arr[0];
for (int i = 0; i < arr.length; i++)
{
if(arr[i] > max)
max = arr[i];
}
System.out.println("Largest element present in given array: " + max);
}
}