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 programEnter the array size:5Enter the array elements:12345Reverse of array is:54321