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