86.8k views
5 votes
How to write a program in java that finds the max number of an array of integers?

User Kofi
by
4.6k points

1 Answer

6 votes

Answer:

The following steps describes how to write a Java program that finds the maximum of an array of integers

STEP 1: Create the array

STEP 2: Intialize the array by assigning integers values of have the user enter the elements of the array using the Scanner class.

STEP 3: Create a temp variable and call it maxInt assign this variable to the element at index 0 of the array

STEP 4: Using a for loop Statement, iterate over every element of the array and check if they are greater than the maxInt variable which is at index 0.

STEP 5: Reassign the maxInt to a new element when ever a greater integer is found

STEP 6. Return or Printout the value of maxInt

The method below written in java shows all the steps listed above to find the max integer in an integer array

Step-by-step explanation:

public static int getMax(int[] arr){

int maxInt = arr[0];

for(int i=1;i < arr.length;i++){

if(arr[i] > maxInt){

maxInt = arr[i];

}

}

return maxInt;

}

User Alexkaessner
by
3.8k points