Program to Reverse an Array.

Program to Reverse an Array

Reverse an Array

Reversing an array is one of the frequently asked questions by many companies and also in competitive programming. So here, I come with a easy solution for reversing an array in easy way.

Steps:
  • Start the loop from size of array -1.
  • (Let n is size of array so we will start loop i = n-1 i.e loop will be started from last of array.)
  • Value of i should be always greater than 0.
  • Then decrement i (i--).
  • At last print the reversed Arrays.

ADVERTISEMENT

CODE:

public class Reverse_Array {
public static void main(String[] args) {
int arr[] = {1,2,3,4,5,6,7,8};
int n = arr.length;
reverseArray(arr,n);
}
static void reverseArray(int arr[], int n)
{
System.out.print("Reversed Array is: ");
for(int i=n-1; i>=0; i--) {
System.out.print(arr[i]+" ");
}
}
}

1 Comments

  1. learn to code python I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks!

    ReplyDelete
Previous Post Next Post

Contact Form