Advertisement
Spocoman

02. Print Numbers in Reverse Order

Oct 20th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int n = Integer.parseInt(scanner.nextLine());
  7.         int[] numbers = new int[n];
  8.  
  9.         for (int i = 0; i < n; i++) {
  10.             numbers[i] = Integer.parseInt(scanner.nextLine());
  11.         }
  12.  
  13.         for (int i = n - 1; i >= 0; i--) {
  14.             System.out.print(numbers[i] + " ");
  15.         }
  16.     }
  17. }
  18.  
  19. OR:
  20.  
  21. import java.util.Scanner;
  22.  
  23. class Main {
  24.     public static void main(String[] args) {
  25.         Scanner scanner = new Scanner(System.in);
  26.         int n = Integer.parseInt(scanner.nextLine());
  27.         String[] numbers = new String[n];
  28.  
  29.         for (int i = n - 1; i >= 0; i--) {
  30.             numbers[i] = scanner.nextLine();
  31.         }
  32.  
  33.         System.out.println(String.join(" ", numbers));
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement