149k views
5 votes
Create a Java program that takes input of a list of integers from the user, stores them into an array and then finally uses the array contents to output the list of integers in reverse. The user's input begins with the number of integers to be stored in the array. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain fewer than 20 integers.

1 Answer

1 vote

Answer:

This question is answered using Java

import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Length of array: ");

int len = input.nextInt();

int[] intArray = new int[len];

for(int i = 0; i<len;i++){

intArray[i] = input.nextInt();

}

for(int i = len-1; i>=0;i--){

System.out.print(intArray[i]+" ");

}

}

}

Step-by-step explanation:

This line prompts user for length of array

System.out.print("Length of array: ");

This gets the length of the arrau

int len = input.nextInt();

This declares the array as integer

int[] intArray = new int[len];

The following iteration gets input to the array

for(int i = 0; i<len;i++){

intArray[i] = input.nextInt();

}

The following iteration prints the array in reversed order

for(int i = len-1; i>=0;i--){

System.out.print(intArray[i]+" ");

}

User Sagar Bommidi
by
5.4k points