C Program to arrange numbers in ascending order

The following program prompts user for the n numbers, once the user is done entering those numbers, this program sorts and displays them in ascending order. Here we have created a user defined function sort_numbers_ascending() for the sorting purpose.

/*
 * C program to accept numbers as an input from user
 * and to sort them in ascending order.
 */
#include <stdio.h>

void sort_numbers_ascending(int number[], int count)
{
   int temp, i, j, k;
   for (j = 0; j < count; ++j)
   {
      for (k = j + 1; k < count; ++k)
      {
         if (number[j] > number[k])
         {
            temp = number[j];
            number[j] = number[k];
            number[k] = temp;
         }
      }
   }
   printf("Numbers in ascending order:\n");
   for (i = 0; i < count; ++i)
      printf("%d\n", number[i]);
}
void main()
{
   int i, count, number[20];
 
   printf("How many numbers you are entering:");
   scanf("%d", &count);
   printf("\nEnter the numbers:\n");
   
   for (i = 0; i < count; ++i)
      scanf("%d", &number[i]);
 
   sort_numbers_ascending(number, count);
}

Output:

How many numbers you are entering: 5
Enter the numbers:
9
8
7
6
5
Numbers in ascending order:
5
6
7
8
9

Recommended Book Must Buy :

To improve your skill and Knowledge you must read Let Us C 16TH EDITION  because this book is totally recommended for beginners and you can improve your skills by attempting example and question inside it 

Note : If you want this book with solution . what you have to do is to click here .




Post a Comment

0 Comments