Program to print Reverse an array in java that is the last element should be displayed first, next second last element and so on.
Using Temp array :-
- Create Scanner class object to pass user input.
- Initialize arr[] size for user input.
- Traverse the for loop to store user input in arr[i].
- Now to print Array in reverse order.
- Now iterate the for loop in reverse order
- Print reverse elements arr[j].
import java.util.Scanner;
public class ReverseArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[20];
System.out.println("Enter the number of elements:- ");
int n = sc.nextInt();
System.out.println("Enter the elements:- ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Array in reverse order :- ");
for (int j = (n - 1); j >= 0; j--) {
System.out.print(arr[j] + " ");
}
}
}
Output :-
Enter the number of elements:-
4
Enter the elements:-
11
12
13
14
Array in reverse order :- 14 13 12 11