This blog is under construction

Tuesday 25 June 2013

C program to sort numbers in ascending and descending order

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


  #include <stdio.h>
  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



16 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Can anyone explain the program

    ReplyDelete
  3. this program is not working they have many error

    ReplyDelete
  4. Thanks for this program I have lot things learn from this program

    ReplyDelete
  5. Thanks for this program I have lot things learn from this program

    ReplyDelete
  6. If only you would explain a little bit more...

    Thanks anyway :)

    ReplyDelete
  7. This programs are very useful and very easiest to submit the project

    ReplyDelete
  8. This programs are very useful and very easiest to submit the project

    ReplyDelete
  9. I am not satisfied this programme.. Because this program understanding is very hard...And this programme not explain properly....

    ReplyDelete
  10. I am not satisfied this programme.. Because this program understanding is very hard...And this programme not explain properly....

    ReplyDelete
  11. Thanks for this program...................

    ReplyDelete
  12. This program has many errors

    ReplyDelete