Java program to reverse array using for loop
Program
import java.util.Scanner;
public class ReverseArrayUsingForLoop {
public static void main(String[] args) {
int i=0;
System.out.println("Enter the required size of the array: ");
Scanner reader = new Scanner(System.in);
int size = reader.nextInt();
int inputArray[] = new int[size];
System.out.println("Enter the elements of the array: ");
for (i = 0; i < size; i++) {
inputArray[i] = reader.nextInt();
}
System.out.println("Array in reverse order: ");
for (i = inputArray.length-1; i >= 0; i--) {
System.out.print(inputArray[i] + " ");
}
}
}
Output
javac .\ReverseArrayUsingForLoop.java
java ReverseArrayUsingForLoop
Enter the required size of the array:
5
Enter the elements of the array:
10
20
30
40
50
Array in reverse order:
50 40 30 20 10