Java Program to Reverse the Second Half Elements of Array

Get array size n and n elements of array, then reverse the second n/2 elements.

Sample Input 1:

5

5 7 9 3 1

Sample Output 1:

5 7 1 3 9

Program or Solution

				
			
					
//To Reverse The Second Half Of Array Elements...

import java.util.*;

class Program
{
  public static void main(String args[])
   {
     int size,i,j;
     Scanner sc=new Scanner(System.in);

     System.out.println("Enter The Size Of The Array:");
     size=sc.nextInt();

     int a[]=new int[size];

    System.out.println("Enter The Array Elements:");

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


    for(i=(size/2),j=size-1;i<j;i++,j--)
      {
        int temp=a[i];
        a[i]=a[j];
        a[j]=temp;
      }

   System.out.println("The Output Is:");
   for(i=0;i<size;i++)
     {
        System.out.println(" "+a[i]);
     }

   }
}

			
				
			

Program Explanation