Java Code to print the elements of an array in reverse order

In this program, the objective is to print the elements of the array in reverse order. This means that the last element should be displayed first, followed by the second-to-last element, and so on.

The Reverse order of the above array is:

Algorithm for a Java Code to print the elements of an array in reverse order

STEP 1: Start

STEP 2: Initialize the array arr[] with values {1, 2, 3, 4, 5}.

STEP 3: Print “Original Array:”.

STEP 4: Repeat STEP 5 for each index i from 0 to the length of arr[].

STEP 5: Print the value at arr[i].

STEP 6: Print “Array in reverse order”.

STEP 7: Repeat STEP 8 for each index i from the length of arr[] – 1 to 0.

STEP 8: Print the value at arr[i].

STEP 9: End.

Java Code to print the elements of an array in reverse order

public class ReverseArray {  
    public static void main(String[] args) {  
        //Initialize array  
        int [] arr = new int [] {1, 2, 3, 4, 5};  
        System.out.println("Original array: ");  
        for (int i = 0; i < arr.length; i++) {  
            System.out.print(arr[i] + " ");  
        }  
        System.out.println();  
        System.out.println("Array in reverse order: ");  
        //Loop through the array in reverse order  
        for (int i = arr.length-1; i >= 0; i--) {  
            System.out.print(arr[i] + " ");  
        }  
    }  
}

Output:

Original array: 
1	2   3   4   5
Array in reverse order:
5    4   3   2   1

In conclusion, the provided Java code is designed to print the elements of an array in reverse order. The code follows a step-by-step approach to achieve this task. It initializes an array with given values and utilizes a loop to iterate through the array in reverse order, printing each element. By implementing this code, one can successfully display the elements of the array in reverse order. Follow tutorials.freshersnow.com to learn more.