Ask Question
20 December, 23:23

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

+2
Answers (1)
  1. 21 December, 02:53
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers