COPY CODE SNIPPET

C Program Sorting of an array in descending order

Sorting of an array in descending order i.e. bottom to top.


  1. # include<stdio.h>
  2. void main()
  3. {
  4.     int arr[5];
  5.     int i=0, temp=0,j=0;
  6.     for(i=0;i<=4;i++)
  7.     {
  8.         printf("Enter number:");
  9.         scanf("%d", &arr[i]);
  10.     }
  11.     temp=arr[0];
  12.     for(j=0;j<=4;j++)
  13.     {
  14.         for(i=0;i<=4;i++)
  15.         {
  16.             if (arr[i+1]>arr[i]) // condition to find out greater number from two neighboring element.
  17.             {
  18.                 temp=arr[i];
  19.                 arr[i]=arr[i+1];
  20.                 arr[i+1]=temp;
  21.             }
  22.         }
  23.     }
  24.     printf("\n Array in descending order is:");
  25.     for(i=0;i<=4;i++)
  26.     {
  27.         printf("\n %d", arr[i]);
  28.     }
  29.     getch();
  30. }
C Program Sorting of an array in descending order C  Program Sorting of an array in descending order Reviewed by Unknown on 16:59 Rating: 5

No comments:

COPY CODE SNIPPET
Powered by Blogger.