Arrange the array elements in descending order using array in C


This program is a C program that uses a for loop to sort an array of numbers in descending order. The program starts by declaring and initializing some variables, including an array called "a" with a size of 10. It then prompts the user to enter the number of elements in the array, and to enter the values of the elements.

The program then enters a nested for loop, where the outer for loop starts at the first element of the array and iterates through all elements, while the inner for loop starts at the first element of the array and iterates through all elements again. The inner loop compares each element of the array with the element of the outer loop, and if the element of the outer loop is greater than the element of the inner loop, it swaps the two elements.

After the nested for loop completes, the program then prints out the sorted array in descending order. This program uses bubble sorting algorithm to sort the array of numbers in descending order

Source Code

#include<stdio.h>
int main()
{
  int s=0,i,n,a[10],t=0,j;
  printf("ENTER LIMIT:");
  scanf("%d",&n);
  printf("ENTER VALUES:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
  {
    for(j=0;j<n;j++)
    {
      if(a[i]>a[j])
      {
        t=a[i];
        a[i]=a[j];
        a[j]=t;
      }
    }
  }
  printf("THE DESCENDING ORDER IS");
  for(i=0;i<n;i++)
  {
    printf("\n%d",a[i]);
  }
  return 0;
}
To download raw file Click Here

Output

ENTER LIMIT:5
ENTER VALUES:7
2
4
1
6
THE DESCENDING ORDER IS
7
6
4
2
1


List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C