Ask Question
3 August, 14:33

How to write a program in java that finds the max number of an array of integers?

+4
Answers (1)
  1. 3 August, 15:14
    0
    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

    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;

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “How to write a program in java that finds the max number of an array of integers? ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers