Sunday, May 12, 2019

Java Array Program - Print a given array in reverse order as entered

Task
Given an array, , of  integers, print 's elements in reverse order as a single line of space-separated numbers.
Input Format
The first line contains an integer,  (the size of our array).
The second line contains  space-separated integers describing array 's elements.
Constraints
  • , where  is the  integer in the array.
Output Format
Print the elements of array  in reverse order as a single line of space-separated numbers.

JAVA Program
import java.util.*;
public class ArrayExample {
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i=0; i < n; i++){
            arr[i] = in.nextInt();
        }
        for(int j=n-1;j>=0;j--){
            System.out.print(arr[j]+" ");
  }
        in.close();
    }
}

2 comments:

  1. Thanks to share these details it’s truly nice.
    ux design firm san francisco

    ReplyDelete
  2. Thanks for sharing the best information and suggestions, If you are looking for the best Tree Interview Questions, then TutorialCup. Highly energetic blog, I’d love to find out some additional information.

    ReplyDelete