Saturday, October 15, 2011

Sorting Program: Descending Order using C programming

This program is written in C programming language. It accept 10 inputs (You can edit the FOR LOOP portion depending on your required number of inputs) and display the numbers in descending order.

Program Source code.

#include <stdio.h>
#include <conio.h>
int main()
{
    int a[10],i,j,temp=0;
    printf("Enter all the 10 numbers\n");
    for(i=0;i<10;i++)
    scanf("%d",&a[i]);
       for(i=0;i<10;i++)  //This loop is for total array elements (n)
      {
          for(j=0;j<9;j++) //this loop is for total combinations (n-1)
          {
                    if(a[j]<a[j+1]) //if the first number is bigger then swap the two numbers
                    {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                    }
      }
    }
printf("The ordered array is\n");
for(j=0;j<10;j++) //Finally print the ordered array
printf("%d \t",a[j]);
getch();
return 0;
}

No comments:

Post a Comment

 

blogger templates | Make Money Online