find maximum number in array using max() function In JAVA

This example demonstrate how to take the input in array and find the maximum value from the given array using the max() function in java

import java.io.DataInputStream;

public class FindMaxValue {
public static void main(String[] args) {
try {
DataInputStream in = new DataInputStream(System.in);
System.out.print(“How many value you want to enter :- “);
int NumberOfValue = Integer.parseInt(in.readLine());

// Input data into array

Integer array[] = new Integer[NumberOfValue];
for (int i = 0; i < NumberOfValue; i++) {
System.out.print(“array[” + i + “]” + ” = “);
array[i] = Integer.parseInt(in.readLine());
}

// Maximum value logic
int MaximumValue = array[0];

for (int i = 1; i < NumberOfValue; i++) {
MaximumValue = Math.max(MaximumValue, array[i]);
}
System.out.println(“Maximum value in the Array = ” + MaximumValue);
} catch (Exception e) {
System.out.println(e);
}
}
}

output of the above program

How many value you want to enter :- 6
array[0] = 23
array[1] = 0
array[2] = 44
array[3] = 56
array[4] = 77
array[5] = 33
Maximum value in the Array = 77