Loading
Reverse the array-tutorial
Reversing an array means printing its elements from the last index to the first. This is a common operation in array manipulation.



Key Point

  • Arrays in Java are index-based, starting from 0.
  • Reversing is done by iterating the array from the last index to the first.
  • Useful in algorithms, data processing, and pattern-based problems.



Example: Reverse an Array

import java.util.Scanner;

public class ReverseArray {
    public static void main(String[ ] args) {

        System.out.println("Reverse the array program");

        Scanner input = new Scanner(System.in); // Taking input

        System.out.println("Enter the array size:");
        int num = input.nextInt();

        int[] arr = new int[10]; // Array declaration and initialization

        System.out.println("Enter the array elements:");
        for (int i = 0; i < num; i++) {
            arr[i] = input.nextInt(); // Taking array elements input
        }

        System.out.println("Reverse of array is:");
        for (int j = num - 1; j >= 0; j--) {
            System.out.println(arr[j]); // Printing array in reverse order
        }
    }
}

Output:

Reverse the array program
Enter the array size:
5
Enter the array elements:
1
2
3
4
5
Reverse of array is:
5
4
3
2
1