Skip to content

Instantly share code, notes, and snippets.

@abdulateef
Last active May 14, 2021 14:33
Show Gist options
  • Save abdulateef/d915b724d9a50107e9b1507848c3a375 to your computer and use it in GitHub Desktop.
Save abdulateef/d915b724d9a50107e9b1507848c3a375 to your computer and use it in GitHub Desktop.
Given an array, A, of N integers, print A's elements in reverse order as a single line of space-separated numbers.
import java.util.Scanner;
public class Reverse{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// System.out.println("Enter the size of the array");
int n = input.nextInt();
int[] arr = new int[n];
// System.out.println("Enter array values");
for(int i=0; i<n; i++){
arr[i]=input.nextInt();
}
// System.out.println("Enter reversed array");
for(int i=(arr.length-1); i>=0; --i){
System.out.print(arr[i]+ " ");
}
}
}
@dakshithadissanayaka
Copy link

dakshithadissanayaka commented May 14, 2021

Full code for this

import java.io.;
import java.util.Scanner;
import java.math.
;
import java.security.;
import java.text.
;
import java.util.;
import java.util.concurrent.
;
import java.util.function.;
import java.util.regex.
;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

    Scanner input = new Scanner(System.in);
     int n = input.nextInt();
     int[] arr = new int[n];
     for(int i=0; i<n; i++){
         arr[i]=input.nextInt();
     }        
     for(int i=(arr.length-1); i>=0; --i){

         System.out.print(arr[i]+ " ");
     }

    bufferedReader.close();
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment