Java Program to Reverse Array

« Previous Program Next Program »

Reverse Array

To reverse an array in Java Programming, you have to ask to the user to enter the array size and then the array elements. Now start swapping the array elements. Make a variable say temp of the same type. And place the first element in the temp, then the last element in the first, and temp in the last and so on.

Java Programming Code to Reverse an Array

Following Java Program ask to the user to enter the size of the array then ask to enter the array elements to make the array reverse, then display the reverse of the array on the screen:

/* Java Program Example - Reverse an Array */
import java.util.Scanner;

public class JavaProgram
   public static void main(String args[])
       int size, i, j, temp;
       int arr[] = new int[50];
       Scanner scan = new Scanner(;
       System.out.print("Enter Array Size : ");
       size = scan.nextInt();
       System.out.print("Enter Array Elements : ");
       for(i=0; i<size; i++)
           arr[i] = scan.nextInt();
       j = i - 1;     // now j will point to the last element
       i = 0;         // and i will point to the first element
           temp = arr[i];
           arr[i] = arr[j];
           arr[j] = temp;
       System.out.print("Now the Reverse of Array is : \n");
       for(i=0; i<size; i++)
           System.out.print(arr[i]+ "  ");

When the above Java Program is compile and executed, it will produce the following output:

Java Program to reverse array

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:

Java Online Test

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.