Ask Question
29 May, 03:05

Java Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. 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. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 To achieve the above, first read the integers into an array. Then output the array in reverse.

+5
Answers (2)
  1. 29 May, 06:13
    0
    import java. util. Scanner;

    public class Main

    {

    public static void main (String[] args) {

    final int COUNT = 20;

    int[] values = new int1;

    Scanner input = new Scanner (System. in);

    System. out. print ("Enter the numbers: ");

    for (int i=0; i
    values[i] = input. nextInt ();

    }

    for (int i=0; i
    int temp = values[i];

    values[i] = values[values. length - i - 1];

    values[values. length - i - 1] = temp;

    }

    for (int value:values) {

    System. out. print (value + " ");

    }

    }

    }

    Explanation:

    - Initialize a constant variable for the size of the array

    - Initialize the array called values

    - Ask the user for the numbers

    - Put entered numbers into the array in the first for loop

    - Swap the values in the second for loop

    - Print the values in for each loop
  2. 29 May, 06:32
    0
    import java. util. Scanner;

    public class num5 {

    public static void main (String[] args) {

    Scanner in = new Scanner (System. in);

    / / Receiving the length of the array

    System. out. println ("Enter the number of elements in the array");

    int num = in. nextInt ();

    / / Declaring the array

    int [] intArray = new int[num];

    int i;

    //Receiving Values into the array

    for (i=0; i
    System. out. println ("Enter the values");

    intArray[i] = in. nextInt ();

    }

    //Printing the Initial Array

    System. out. println ("The original arrangement of the array");

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

    }

    System. out. println ();

    //Calling Method reverseArray

    System. out. println ("The Reversed array is");

    reverseArray (intArray, num);

    }

    static void reverseArray (int a[], int n)

    {

    int[] reverseArray = new int[n];

    int j = n;

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

    reverseArray[j - 1] = a[i];

    j = j - 1;

    }

    for (int k = 0; k < n; k++) {

    System. out. print (reverseArray[k]+" ");

    }

    }

    }

    Explanation:

    Use Scanner class to prompt user for a length for the array int num Create an array of integers of size num Use a for loop to continually ask user to enter values into this array. Use another for loop to display the fully quantified array Define a method reverseArray static void reverseArray (int a[], int n) That accepts two parameters the array and its length and reverses the position of the elements pf the array Call the method with the main method
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Java Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the ...” 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