Pages

Wednesday 11 December 2019

Program to reverse the given array elements


import java.util.Scanner;

class rkrev
 {
  public static void main(String x[])
  {
   int n,i,temp;
   Scanner s=new Scanner(System.in);
   System.out.println("Enter Size Of Array :");
   n=s.nextInt();
   int[] a;//declaration of array
   a=new int[n]; // Creation of array with given size
   for(i=0;i<n;i++)
    {
     System.out.printf("\n Element a[%d]  :",i);
     a[i]=s.nextInt();
    }
   System.out.print("\n Array elements before reverse :");
   for(i=0;i<n;i++)
    {
     System.out.print(a[i]+" ");
    }
   for(i=0;i<n;i++)
    {
     System.out.printf("\n\n ADDRESS=%h ---- ARRAY=a[%d]------ DATA=%d",i*4,i,a[i]);
    }
   for(i=0;i<n/2;i++)
    {
     temp=a[i];
     a[i]=a[n-1-i];
     a[n-1-i]=temp;
    }
   System.out.print("\n Array elements after reverse :");
   for(i=0;i<n;i++)
    {
     System.out.print(a[i]+" ");
    }
   for(i=0;i<n;i++)
    {
     System.out.printf("\n\n ADDRESS=%h ---- ARRAY=a[%d]------ DATA=%d",i*4,i,a[i]);
    }
  }
}

output:



No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...