Array Maximum Element

This program finds the maximum element in an array.

Problem Statement

Write a Java program to find the maximum element in an integer array.

Source Code

1 public class ArrayMax {
2 public static void main(String[] args) {
3 int[] arr = {12, 45, 67, 23, 89, 34};
4 int max = arr[0];
5 for(int i=1; i<arr.length; i++) {
6 if(arr[i] > max) max = arr[i];
7 }
8 System.out.println("Maximum element: " + max);
9 }
10 }

Program Output

Maximum element: 89

Explanation

The variable max starts with the first element, and the for loop compares each subsequent element with max, updating it whenever a larger value is found, so the final result prints the maximum element, demonstrating a comparison operation on arrays.

Quick Links to Explore