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);
}
}
Largest element of the array: 9
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
. max
, we update the value of max
. Finally, we print the value of max
, which is the largest element of the array.