## Write a C program to sort numbers in ascending and descending order

Here in this article, I shared c program to sort numbers in ascending and descending order and also we use array concept to solve the problem.
Describe the program briefly? So, you easily understand the below program.
Firstly, I will clarify that I use the 30 elements array. so, you can't exceed this limit and if you want to exceed then you edit by your self. When you compile and execute the program it will ask for how many value of N, where N is the length of the array and then enter elements randomly in the array. I use switch case that will ask to enter your choice and show accordingly to it.

## C program to arrange numbers in ascending & descending order

### Source Code:

#include<stdio.h>

int main()
{
int i,j,a,n,number,ch;
printf("Enter the value of N");
scanf("%d",&n);
printf("Enter the numbers\n");
for(i=0;i<n;i++)
{
scanf("%d",&number[i]);
}
while(1)
{
printf("Press 1. For ASCENDING ORDER\n");
printf("Press 2. For DESCENDING ORDER\n");
printf("Press 3. EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1: 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("\nThe numbers arranged in ascending order are given below\n");
for(i=0;i<n;i++)
{
printf("%d\n",number[i]);
}
break;
case 2:   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("\nThe numbers arranged in descending order are given below\n");
for(i=0;i<n;i++)
{
printf("%d\n",number[i]);
}
break;
default:printf("\n INVALID CHOICE \n");
break;
}
}
return 0;
}

### Output:

```
Enter the value of N: 5```
```Enter the numbers
15
65
80
10
101```
```Press 1. For ASCENDING ORDER
Press 2. For DESCENDING ORDER
Press 3. EXIT
1
The numbers arranged in ascending order are given below
10
15
65
80
101```
```Press 1. For ASCENDING ORDER
Press 2. For DESCENDING ORDER
Press 3. EXIT
2
The numbers arranged in descending order are given below
101
80
65
15
10
```
```Press 1. For ASCENDING ORDER
Press 2. For DESCENDING ORDER
Press 3. EXIT
4
INVALID CHOICE
Press 1. For ASCENDING ORDER
Press 2. For DESCENDING ORDER
Press 3. EXIT