Java Program to Sort the Elements in descending order.

Get array size n and n elements of array, then sort the elements of array in descending order.

Sample Input 1:

5 5 7 9 3 1

Sample Output 1:

9 7 5 3 1

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

public class Hello { public static void main(String args[]) { //Write your code here } }

Program or Solution

				
			
					
import java.util.*;
class Descending_Order
{
  public static void main(String args[])
  {
     int size,i,j,num,temp;
     Scanner sc=new Scanner(System.in);
     System.out.println("Enter Size Of Array:");
     size=sc.nextInt();
     int a[]=new int[100];
     System.out.println("Enter The Array Elements:\n");
     for(i=0;i<size;i++)
        {
            a[i]=sc.nextInt();

	}

    for(i=0;i<size;i++)
	{
		for(j=i+1;j<size;j++)
		{
			if(a[i]<a[j])
			{
				temp=a[i];
				a[i]=a[j];
				a[j]=temp;
			}
		}
	}
	System.out.println("After Sorting It Is In Descending Order:");
	for(i=0;i<size;i++)
	{
	     System.out.println(""+a[i]);
	}
  }
}
			
				
			

Program Explanation

Comments