C program to sort given numbers in descending order:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
#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]); } |
descending order of entered numbers
7
6
4
3
1
Note:
In above C program, fixed numbers are used to sort in descending order. If you want to get inputs from keyboard, please use below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#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.