Write a C program to sort numbers in ascending and descending order.
int main() {
int n, data[100], i, j, temp;
/* get the number of entries */
printf("Enter your input for n:");
scanf("%d", &n);
/* get the input data */
for (i = 0; i < n; i++)
scanf("%d", &data[i]);
/* sort the given data in ascending order */
for (i = 0; i < n-1; i++) {
for (j = i + 1; j < n; j++) {
if (data[i] > data[j]) {
temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
}
/* data in ascending order */
printf("Ascending Order:\n");
for (i = 0; i < n; i++)
printf("%d\n", data[i]);
/* data in descending order */
printf("\nDescending Order:\n");
for (i = n-1; i >= 0; i--)
printf("%d\n", data[i]);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input for n:5
300
100
500
200
400
Ascending Order:
100
200
300
400
500
Descending Order:
500
400
300
200
100
Enter your input for n:5
300
100
500
200
400
Ascending Order:
100
200
300
400
500
Descending Order:
500
400
300
200
100
This comment has been removed by the author.
ReplyDeleteCan anyone explain the program
ReplyDeletethis program is not working they have many error
ReplyDeleteThanks for this program I have lot things learn from this program
ReplyDeleteThanks for this program I have lot things learn from this program
ReplyDeleteIf only you would explain a little bit more...
ReplyDeleteThanks anyway :)
This programs are very useful and very easiest to submit the project
ReplyDeleteThis programs are very useful and very easiest to submit the project
ReplyDeleteI am not satisfied this programme.. Because this program understanding is very hard...And this programme not explain properly....
ReplyDeleteI am not satisfied this programme.. Because this program understanding is very hard...And this programme not explain properly....
ReplyDelete??flow chart&algorithm
ReplyDeleteProgram to Multiply Two Matrices
ReplyDeletec language sample codes
Really good program
ReplyDeleteThankyouuuu
ReplyDeleteThanks for this program...................
ReplyDeleteThis program has many errors
ReplyDelete