Question
print array elements in reverse order.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class arrayElementsinReverseOrder
{
public static void main(String[] args)
{
/* Initialize array */
int [] arr;
int n=0,i=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no. of elements in the array");
n = sc.nextInt();
System.out.println("Enter numbers in array");
arr=new int[n];
for(i=0;i< n;i++)
{
arr[i]=sc.nextInt();
}
System.out.println("Original array: ");
for ( i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println();
System.out.println("Array in reverse order: ");
/* Loop through the array in reverse order */
for (i = arr.length-1; i >= 0; i--)
{
System.out.print(arr[i] + " ");
}
}
}