In this post, we will learn how to arrange numbers in ascending order using C Programming language.
This program asks the user to enter n elements, then this program sorts and arrange the entered numbers in the ascending order.
We use two methods to write this program. These two methods are as follow:-
- Using Standard Method
- Using User-defined function
So, without further ado, let’s begin this tutorial.
C Program To Arrange Numbers in Ascending Order
// C Program To Arrange Numbers in Ascending Order #include <stdio.h> int main(){ int num[25]; int a, j, i, n; // Asking for input printf("Enter total no. of elements: "); scanf("%d", &n); printf("Enter the numbers one by one\n"); for (i = 0; i < n; ++i){ scanf("%d", &num[i]); } for (i = 0; i < n; ++i){ for (j = i + 1; j < n; ++j){ if (num[i] > num[j]){ a = num[i]; num[i] = num[j]; num[j] = a; } } } printf("Numbers in ascending order: \n"); for (i = 0; i < n; ++i){ printf("%d\n", num[i]); } return 0; }
Output
Enter total no. of elements: 5
Enter the numbers one by one
12
17
5
32
44
Numbers in ascending order:
5
7
12
32
44
How Does This Program Work ?
int num[25];
int a, j, i, n;
In this program, we have defined four int data type variables named as a, j, i and n.
// Asking for input
printf("Enter total no. of elements: ");
scanf("%d", &n);
printf("Enter the numbers one by one\n");
for (i = 0; i < n; ++i){
scanf("%d", &num[i]);
}
Then, the user is asked to enter the total no. of elements which the user wants to arrange in the ascending order.
for (i = 0; i < n; ++i){
for (j = i + j; j < n; ++j){
if (num[i] > num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
Now, We sort the elements of the array in ascending order.
printf("Numbers in ascending order: \n");
Finally, the numbers are printed in the ascending order using printf() function.
C Program To Arrange Numbers in Ascending Order Using User-defined function
// C Program To Arrange Numbers in Ascending Order #include <stdio.h> int arrange(int num[], int count){ int i, j, k, temp; for (i = 0; i < count; ++i){ for (j = i + 1; j < count; ++j){ if (num[i] > num[j]){ temp = num[i]; num[i] = num[j]; num[j] = temp; } } } printf("Numbers in ascending order: \n"); for (k = 0; k < count; ++k){ printf("%d\n", num[k]); } } int main(){ int i, count, num[25]; // Asking for Input printf("Enter total no. of elements: "); scanf("%d", &count); printf("Enter elements one by one: \n"); for (i = 0; i < count; ++i){ scanf("%d", &num[i]); } arrange(num, count); return 0; }
Output
Enter total no. of elements: 3
Enter elements one by one:
25
448
17
Numbers in ascending order:
17
25
448
Conclusion
I hope after going through this post, you understand how to arrange numbers in ascending order in C Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read:
How can we write a program to read the 3 numbers an arrange them in ascending order using nested if statements in C programming?
How can we write a program to read the 3 numbers an arrange them in ascending order and descending order using nested if statements in C programming?