Saturday, 10 January 2015

Java Program to reverse all the elements in an Array

This is a Java Program which reverses all the elements present in an array.

Here we read  all the elements from the array and again read them from last element and print them back.

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class ReverseArray {
 public static void main(String[] args) {
  int i, n, c, d;
  Scanner scan = new Scanner(System.in);
  System.out.print("How many numbers? \t");
  n = scan.nextInt();
  int a[] = new int[n];
  int b[] = new int[n];
  for (i = 0; i < n; i++) {
   System.out.print("Enter number " + (i + 1) + "\t");
   a[i] = scan.nextInt();
  }
  scan.close();
  System.out.print("\n\nOriginal array is :\t");
  for (i = 0; i < n; i++) {
   System.out.print(a[i] + "\t");
  }

  for (c = n - 1, d = 0; c >= 0; c--, d++)
   b[d] = a[c];

  for (c = 0; c < n; c++)
   a[c] = b[c];

  System.out.print("\n\nReversed array is :\t");

  for (c = 0; c < n; c++)
   System.out.print(a[c] + "\t");
 }
}

OUTPUT :
Java - Reverse the elements in an array