# C++ Program To Arrange Numbers in Ascending Order Using Array Sorting

In ascending order, numbers are arranged from the smallest to the largest number. For example:

**5 13 25 39 50 65 75 86 99 100**

This C++ program to arrange numbers in ascending order is aimed to sort an array.

In this program, A user will input 5 random numbers in array and then program will arrange numbers in ascending order.

## C++ Source Code To Arrange Numbers in Ascending Order :

#include<iostream> #include<conio.h> using namespace std; void order(int arr[], int count); int main() { int array[5]; cout << "Enter Array Elements (MAX 5) "<<endl; cout<<"=============================="<<endl; for (int i = 0; i < 5; i++) { cin >> array[i]; } order(array, 5); return 0; } void order(int arr[], int count) { int temp; for (int i = 0; i < count; i++) { for (int j = i + 1; j < count; j++) { if (arr[i] > arr[j]) { temp= arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } cout<<"=============================="<<endl; cout <<"Ascending Order:"; for (int w = 0; w < count; w++) { cout << arr[w]<<" "; } }

## Program Output:

## Code Explanation:

First of all we used the header files and the libraries which are going to be used in the given program.

Before diving into the function **order** , first we see our **main() ** function.

In **main()** function, we created an array of **5** elements and asked the user to enter numbers in array.

After receiving input from user, we passed this array to **order** function which arranges numbers in ascending order and displays them on screen.

We created a function **order(int arr[] , int count)** in which we will write the code to sort array elements in ascending order.

There are two parameters of **order** function:

First is denoted by

**arr[]**and it is an array of integer type variables.Second is

**count**and it contains the length of array which is**5**.

Then we defined an **int** type variable named **temp**. This will help us to swap the numbers which is core of this sorting technique.

Then we used **FOR** loop and introduced an integer **i **and incremented it with **1** for each cycle.

Next we created nested **FOR** loop and introduced integer **j** with initial value **i+1**.

Inside loop, we used **IF** condition to compare whether the element at **i**th position is greater or the element at **j**th position.

If number at **j**th position is greater then it means that number is at its correct place.

If number at **i**th position is greater then we swap both numbers.

I highly recommend you to __read this tutorial about swapping__ of two numbers.

After sorting the complete array using swapping , we are going to print numbers on screen.

We used **FOR** loop to display all the numbers on screen.

Finally your C++ program to arrange numbers is completed and now you can run your program and get the output.

If you think that this article is a bit useful then please share it with your friends on __Twitter__ and __Facebook__.

If you have any question then you can ask me in comments. I would like to answer them.

You can also Click Here To Download Exercise Files.

## Follow Us