Java Program to reverse an array elements


In this program, You will learn how to reverse array elements in java.


List is: 1 2 3 4 5

After reverse List is: 5 4 3 2 1

Example: How to reverse array elements in java.

import java.util.Scanner;

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

        int i, j, temp;
        int arr[] = new int[5];
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter 5 numbers:");

        for (i = 0; i < 5; i++) {
            arr[i] = sc.nextInt();
        }

        j = 5 - 1;
        for (i = 0; i < j; i++) {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            j--;
        }

        System.out.print("After Reverse list:");
        for (i = 0; i < 5; i++) {
            System.out.print(" " + arr[i]);
        }

    }
}

Output:

Enter 5 numbers:10 20 30 40 50
After Reverse list: 50 40 30 20 10

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.