Top MNC interview question which will be very useful for beginners to crack the interview and for there future careers

test

Breaking

Post Top Ad

Thursday, January 21, 2021

C program to Sort the Array Eements in Descending Order

 Question:

C program to Sort the Array Eements in Descending Order

Explanation:

We need to sort the elements in descending order

For Example:

            Array [1,2,3,4,5]

            O/P: 5,4,3,2,1

Program:

#include <stdio.h>
    void main ()
    {
 
        int number[30];
 
        int i, j, a, n;
        printf("Enter the value of N\n");
        scanf("%d", &n);
 
        printf("Enter the numbers \n");
        for (i = 0; i < n; ++i)
        scanf("%d", &number[i]);
 
        /*  sorting begins ... */
 
        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("The numbers arranged in descending order are given below\n");
 
        for (i = 0; i < n; ++i) 
        {
            printf("%d\n", number[i]);
        }
 
    }

No comments:

Post a Comment

Post Top Ad