Programming Examples

Java program to reverse the array


Write a java program which accept a array from user and revere the array and print it.

import java.util.*;
class ReverseArray { 
    
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the size of Array : ");
        int size=sc.nextInt();
        int arr[]=new int[size];
        System.out.println("Enter "+size+" Elements for Array :");
        for(int i=0;i<size;i++)
        {
            arr[i]=sc.nextInt();
        }       
        System.out.println("Original array: ");
        for(int i=0;i<size;i++)
        {
            System.out.print(arr[i]+"  ");
        }
        int a,b;
        for(a=0,b=size-1;a<b;a++,b--)
        {
            int temp = arr[a];
            arr[a] = arr[b];
            arr[b] = temp;    
        }
        
        
        System.out.println("\nReversed array: ");
        for(int i=0;i<size;i++)
        {
            System.out.print(arr[i]+"  ");
        }
    }
}
Output

Enter the size of Array : 

10

Enter 10 Elements for Array :

2

3

4

5

6

7

8

9

10

1

Original array: 

2  3  4  5  6  7  8  9  10  1  

Reversed array: 

1  10  9  8  7  6  5  4  3  2  

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz