Sort numbers in descending order : C Programming

Q. Write a program to sort the numbers entered by user in descending order using C Programming
==>
Code goes like this :
/* Program for descending order */
#include<stdio.h>
int main()
{
    int i,j,num,a[50],temp;
    printf("Enter size of array:");
    scanf("%d",&num);
    printf("Enter %d elements to sort:",num);
    for(i=0;i<num;i++)
    {
        scanf("%d",&a[i]);
    }                           // Taking values till now
    for(i=0;i<num-1;i++)
    {
        for(j=i+1;j<num;j++)
        {
            if(a[i]<a[j])           // < for descending
            {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
            }
        }
    }                            // swapping values for sorting
    printf("\n The descending order is:");
    for(i=0;i<5;i++)
    {
        printf(" %d",a[i]);    // Displaying values
    }
return 0;   
}

Output: 














I have used Dev CPP to compile this program.
If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you 
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment