top of page
Click here to go to the home page of AskTheCode.

Print elements of array in reversed order - Java | AskTheCode

Team ATC

Updated: Mar 9, 2021

Basic Java program to reverse an array | Array in Java | AskTheCode

 

Problem:

We've already made arraying/listing the easy way, but how about arraying/listing and printing the list in reverse order?

Make a program that will input an integer and then using loops, add items on an array/list one by one for the same number of times as that of the first inputted integer. Then, print out the array/list in reverse order, that is, starting from the last item on the array/list down to the first one, each in separated lines.


Input Format:

The first line contains the size of the array/list.

The next line contains the items of the array/list (integers).


Input Sample:

5

1

64

32

2

11


Output Format:

Multiple lines containing integers


Output Sample:

11

2

32

64

1


Code:

import java.util.Scanner;

public class Rev_Array{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt();
		int[] arr = new int[a];

		for (int i = 0; i < a; i++) {
			arr[i] = sc.nextInt();
		}

		for (int i = a - 1; i >= 0; i--) {
			System.out.println(arr[i]);
		}
	}
}

Recent Posts

See All

Komentar


bottom of page