Pages

Tuesday, 17 December 2019

Program to illustrate Descending order of n array elements

Program to illustrate Descending order of n array elements

import java.util.Scanner;
public class Rkdes
 {
  public static void main(String[] args)
  {
   int n,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(int i=0;i<n;i++)
       {
        System.out.printf("enter a[%d] = ",i);
        a[i]=s.nextInt();
       }
   System.out.print("ARRAY before Descending order :\n");
   for(int i=0;i<n;i++)
       {
        System.out.print( a[i] +" ");
       }
   for(int i=0;i<n;i++)
    {
     for(int j=0;j<n-1;j++)
      {
        if(a[j]<a[j+1])
        {
         temp=a[j];
         a[j]=a[j+1];
         a[j+1]=temp;
        }
      }
     }
  System.out.print("\nARRAY after Descending order :\n");
    for(int i=0;i<n;i++)
       {
        System.out.print( a[i] +" ");
       }
  
for(int i=0;i<n;i++)
  System.out.printf("\nAddress:%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...