Loading
Data-structure-with-JAva-Tutorial

Reverse the array

 Q. Print the program in reverse order of a given array.


Example:-

input -  1,2,3,4,5
output-  5,4,3,2,1

Program Solution:-


import java.util.Scanner;

public class ReverseArray
{
	public static void main(String[] args) {
	
	System.out.println("Reverse the array program");
	Scanner input=new Scanner(System.in);//Taking input
	System.out.println("Enter the array size");
	int num = input.nextInt();
	
	int[] arr=new int[10];//initialize and declaration of array.
	
	System.out.println("Enter the array value");
	for(int i=0;i<num;i++)
	{
	    arr[i]=input.nextInt();//taking array values input
	}
	System.out.println("Reverse of array is : ");
	for (int j=num-1;j>=0;j--)
	{
	    System.out.println(arr[j]);//printing the reverse value
	}
	}
}

Output:

Reverse the array program
Enter the array size
5
Enter the array value
1
2
3
4
5
Reverse of array is :
5
4
3
2
1