Write a Java program to reverse an array.

Suman Barik
0

 public class MyClass {

    public static void main(String args[]) {

      int [] arr = {1, 21, 3, 4, 5, 34, 67};

        int l = arr.length;

        int n = Math.floorDiv(l, 2);

        int temp;


        for(int i=0; i<n; i++){

            // Swap a[i] and a[l-1-i]

            // a   b   temp

            // |4| |3| ||

            temp = arr[i];

            arr[i] = arr[l-i-1];

            arr[l-i-1] = temp;

        }


        for(int element: arr){

            System.out.print(element + " ");

        }

    }

}


//output - 67 34 5 4 3 21 1 

Post a Comment

0Comments
Post a Comment (0)