Ask Question

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

+1
Answers (1)
  1. 31 July, 12:09
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers