To sort the given numbers in ascending & descending order
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], t, i, j, n;
clrscr();
printf("Enter the limit : ");
scanf("%d", &n);
printf("\nEnter the numbers :\n\n");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
for(j = 0; j < n - 1; j++)
if(a[j] > a[j + 1])
{
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
printf("\nThe numbers in ascending order is :\n\n");
for(i = 0 ; i < n ; i++)
printf("%d\t", a[i]);
printf("\n\nThe numbers in descending order is :\n\n");
for(i = n -1 ; i >= 0 ; i--)
printf("%d\t", a[i]);
getch();
}
Output:
Enter the limit : 5
Enter the numbers :
20 30 10 50 40
The numbers in ascending order is :
10 20 30 40 50
The numbers in descending order is :
50 40 30 20 10
-
UpdatedDec 31, 2019
-
Views34,098
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order