Java Program to Reverse an Array

This article covers multiple programs in Java, to reverse an array, entered by user at run-time of the program. List of programs included in this article are:

Print Reverse of an Array in Java

The question is, write a Java program to print the reverse of an array. The array must be received by user. The program given below is its answer:

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      int i;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Size of Array: ");
      int tot = scan.nextInt();
      int[] arr = new int[tot];
      
      System.out.print("Enter " +tot+ " Elements for the Array: ");
      for(i=0; i<tot; i++)
         arr[i] = scan.nextInt();
      
      System.out.println("\nReverse of Given Array is: ");
      for(i=(tot-1); i>=0; i--)
         System.out.print(arr[i]+ " ");
   }
}

The snapshot given below shows the sample run of above program with user input 6 as size and 1, 2, 3, 4, 5, 6 as six elements:

java print reverse of an array

Reverse an Array in Java using for Loop

Since previous program does not actually reverses the array, rather that program only prints the reverse of an array. But this program first reverse the given array, and then prints the array, using of course for loop

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      int i, j, x;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Size of Array: ");
      int tot = scan.nextInt();
      int[] arr = new int[tot];
      
      System.out.print("Enter " +tot+ " Elements for the Array: ");
      for(i=0; i<tot; i++)
         arr[i] = scan.nextInt();
      
      j = tot-1;
      for(i=0; i<j; i++, j--)
      {
         x = arr[i];
         arr[i] = arr[j];
         arr[j] = x;
      }
      
      System.out.println("\nReverse of Given Array is: ");
      for(i=0; i<tot; i++)
         System.out.print(arr[i]+ " ");
   }
}

You'll get the same output as of previous program.

Reverse an Array in Java using while Loop

This is the last program of this article, created using while loop, instead of for, to find and print the reverse of an array in Java.

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      int i, j, x;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Size of Array: ");
      int tot = scan.nextInt();
      int[] arr = new int[tot];
      
      System.out.print("Enter " +tot+ " Elements for the Array: ");
      i=0;
      while(i<tot)
      {
         arr[i] = scan.nextInt();
         i++;
      }
      
      j=tot-1;
      i=0;
      while(i<j)
      {
         x = arr[i];
         arr[i] = arr[j];
         arr[j] = x;
         i++;
         j--;
      }
      
      System.out.println("\nReverse of Given Array is: ");
      i=0;
      while(i<tot)
      {
         System.out.print(arr[i]+ " ");
         i++;
      }
   }
}

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »