Ask Question

Write a program that prompts the user to enter the number of integer numbers you need to enter, then ask user to enter these integer numbers. Your program should contain an indexOfLargestElement method, which is used to return the index of the largest element in an array of integers.

+5
Answers (1)
  1. 17 March, 06:25
    0
    import java. util. Scanner;

    public class Main

    {

    public static void main (String[] args) {

    Scanner ob = new Scanner (System. in);

    System. out. println ("How many numbers? ");

    int n = ob. nextInt ();

    int[] arr = new int[n];

    for (int i=0; i
    System. out. print ("Enter the number: ");

    arr[i] = ob. nextInt ();

    }

    System. out. println (indexOfLargestElement (arr));

    }

    public static int indexOfLargestElement (int[] arr) {

    int max = arr[0];

    int maxIndex = 0;

    for (int i=0; i
    if (arr[i] > = max) {

    max = arr[i];

    maxIndex = i;

    }

    }

    return maxIndex;

    }

    }

    Explanation:

    Create a method called indexOfLargestElement that takes one parameter, an array

    Initialize the max and maxIndex

    Create a for loop iterates throgh the array

    Check each element and find the maximum and its index

    Return the index

    Inside the main:

    Ask the user to enter how many numbers they want to put in array

    Get the numbers using a for loop and put them inside the array

    Call the indexOfLargestElement method to find the index of the largest element in the array
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program that prompts the user to enter the number of integer numbers you need to enter, then ask user to enter these integer ...” 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