C Program to Sort the Array in an Ascending Order

This C Program sorts array in an ascending order.

Here is source code of the C program to sort the array in an ascending order. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to accept N numbers and arrange them in an ascending order
  3.  */
  4. #include <stdio.h>
  6. void main()
  7. {
  8.     int i, j, a, n, number[30];
  10.     printf("Enter the value of N \n");
  11.     scanf("%d", &n);
  12.     printf("Enter the numbers \n");
  13.     for (i = 0; i < n; ++i)
  14.         scanf("%d", &number[i]);
  15.     for (i = 0; i < n; ++i)
  16.     {
  17.         for (j = i + 1; j < n; ++j)
  18.         {
  19.             if (number[i] > number[j])
  20.             {
  21.                 a =  number[i];
  22.                 number[i] = number[j];
  23.                 number[j] = a;
  24.             }
  25.         }
  26.     }
  27.     printf("The numbers arranged in ascending order are given below \n");
  28.     for (i = 0; i < n; ++i)
  29.         printf("%d\n", number[i]);
  30. }

$ cc pgm66.c
$ a.out
Enter the value of N
Enter the numbers
The numbers arranged in ascending order are given below

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More