Print elements of array in reversed order - Java | AskTheCode

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]);

}

}

}


12 views0 comments

Recent Posts

See All

Calculate BMI in Java | AskTheCode

Write a Java program that reads values for weight in kilograms and height in centimeters and prints out the BMI... interpretation of it...

"Believe you can and you're halfway there"