Discussion Forums

New User? Please read instructions!

C program to sort given numbers in descending order  

  RSS

thiyagarajan.s
Active Member
Joined: 1 month  ago
Posts: 14
27/01/2017 10:14 am  

C program to sort given numbers in descending order


ReplyQuote
prakash
Eminent Member
Joined: 1 month  ago
Posts: 20
30/01/2017 4:27 am  

Dear Thiyagarajan, please use below code for sorting given numbers in descending order.

#include <stdio.h>

int main ()

{

   //int i,j,a,n,number[15];

     int i,j,a,n=5,number[15];

   /*

       printf ("\nPlease enter how many numbers you want to sort");

       scanf (""%d"", &n);

       printf ("\nPlease enter the numbers to be sorted as descending order");

       for (i=0; i<n; ++i)

       scanf ("%d",&number[i]);

   */

   number[0]=4;

   number[1]=6;

   number[2]=3;

   number[3]=7;

   number[4]=1;

 for (i=0; i<n; ++i)

 {

   for (j=i+1; j<n; ++j)

   {

     if (number[i] < number[j])

     {

       a= number[i];

       number[i] = number[j];

       number[j] = a;

     }

   }

 }

 printf ("\ndescending order of entered numbers");

 for (i=0; i<n; ++i)

 printf ("\n%d",number[i]);

}

 

Output:

descending order of entered numbers

7

6

4

3

1

 

Note:

I have used fixed numbers to show you how to sort in descending order.If you want to get inputs from keyboard, please use below code.

#include <stdio.h>

int main ()

{

     int i,j,a,n=5,number[15];

       printf ("\nPlease enter how many numbers you want to sort");

       scanf (""%d"", &n);

       printf ("\nPlease enter the numbers to be sorted as descending order");

       for (i=0; i<n; ++i)

       scanf ("%d",&number[i]);

 for (i=0; i<n; ++i)

 {

   for (j=i+1; j<n; ++j)

   {

     if (number[i] < number[j])

     {

       a= number[i];

       number[i] = number[j];

       number[j] = a;

     }

   }

 }

 printf ("\ndecending order of entered numbers");

 for (i=0; i<n; ++i)

 printf ("\n%d",number[i]);

}

Output:

Numbers will be sorted in descending order based on your entered numbers.

Edited: 4 weeks  ago

ReplyQuote
  
Working

Please Login or Register

Ask your questions or clarify your/others doubts from,

✅ Online Discussion Forums by Fresh2Refresh.com





Share this website !!!
Facebook Twitter Linkedin

Close
We Support JALLIKATTU !!!
We Support JALLIKATTU!
By Fresh2Refresh.com
Show Your Support as well. Thanks!