Java Program to reverse elements of an array

April 16, 2022, Learn eTutorial

Here we are explaining how to write a java program to find out the reverse of an array. Here first we read the limit of the array and read the array elements. Then by using a while loop place the first array element, to the last position, the second element to the second last position, and so on. Then we will display the array as the reverse of the array.

What is the syntax of while loop in java?

The syntax of the while loop is shown below.

while( condition)


// block of code


If the condition is true, then the block of code is executed repeatedly until the condition becomes false.

How to implement the java program to find out the reverse of an array?

First, we have to declare the class ArraySum.Then open the main function. Create an object of the scanner class. Declare an object of the scanner class. Read the limit of the array into the variable n. Then read the numbers into the array array[] using for loop. Then used by another for loop to find out the sum of the array elements as sum=sum+num. After that, display the sum.


STEP 1: Declare the class RevArray with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Initialize an integer array A[] of size 100.

STEP 4: Declare the integer variables l,i,j,temp.

STEP 5: Read the limit of the array into the variable l.

STEP 6: Read the array elements into A[i] using for loop.

STEP 7: Set j=i-1,i=0.

STEP 8: By using a while loop with the condition i

STEP 9: Swap the elements A[i] with a[j] and increment i by one.

STEP 10:Repeate step 8.

STEP 11: Display the reversed array as A[i] using fo loop.


Java Source Code

                                          import java.util.Scanner;
public class RevArray {
    public static void main(String args[]) {

        int A[] = new int[100];
        int l, i, j, temp;

        Scanner scanner = new Scanner(;
        System.out.println("Enter the limit of the array?");
        l = scanner.nextInt();

        System.out.println("Enter the array elements : ");

        for (i = 0; i < l; i++) {

            A[i] = scanner.nextInt();

        j = i - 1;
        i = 0;
        while (i < j) {
            temp = A[i];
            A[i] = A[j];
            A[j] = temp;

        System.out.println("Reversed array Elements: ");
        for (i = 0; i < l; i++) {
            System.out.print(A[i] + "  ");


Enter the limit of the array?
Enter the array elements : 
Reversed array Elements: 
5  4  3  2  1