Site icon TestingDocs.com

Write a Java Program to find the maximum element in an array

Problem Description

Write a Java Program to find the maximum element in an array. We will declare an array with some numbers. Loop each element in the array to find the maximum element in the given array.

Java Program

//********************************
//MaxInArray.java
//********************************

public class MaxInArray {
  //main method
  public static void main(String[] args) {
    int maxValue= Integer.MIN_VALUE;
    int[] arr = {2, 6, 18, 9, 56, 44, 63, 22, 76, 28}; // declare an array of numbers	

    for (int i = 0; i < arr.length; i++) {
      if(arr[i] > maxValue) {
        maxValue = arr[i];
      }
    }
    System.out.print("The maximum element in the array is= " + maxValue); //output max number
  }
}

 

Program Output

The maximum element in the array is= 76

Screenshot

Exit mobile version