In this tutorial, We will learn writing Java program to create an Array and return the elements in Reverse Order.
Our program will first take the input of array size and then the elements of the array. And return the reverse of the input array.
For Example
Case 1: If the user inputs 4 as array (list) size and the array (list) elements as 1,2,3,4.
The output should be 4,3,2,1.
Case 2: If the user inputs 5 as array (list) size and the array (list) elements as 9,8,7,6,5.
The output should be 5,6,7,8,9.
Program 1: Java Program to Print Array in Reverse Order using For Loop
Output
Explanation
The input array size is 5, so the ‘for loop’ will executes the body 5 times taking input from the users as the elements of the array, which is {1, 2, 3, 4, 5}. The program returned the reverse of the input array i.e. {5, 4, 3, 2, 1}.
Program 2: Java Program to Print Array in Reverse Order using While Loop
Output
Explanation
The input array size is 5, so the ‘for loop’ will executes the body 5 times taking input from the users as the elements of the array, which is {1, 2, 3, 4, 5}. The program returned the reverse of the input array i.e. {5, 4, 3, 2, 1}.