Search This Blog

Saturday 23 January 2016

Write a program to arrange an array of N elements into ascending order.

Practical 4

#include<iostream.h>
#include <conio.h>

int main()
{
    int i, j, a, n, num[30];

    cout<<"Enter the value of n:";
    cin>>n;
    cout<<"Enter the numbers:";
    for (i = 0; i < n; ++i)
        cin>>num[i];
    for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (number[i] > number[j])
            {
                a =  num[i];
                num[i] = num[j];
                num[j] = a;
            }
        }
    }
    cout<<"Ascending order:";
    for (i = 0; i < n; ++i)
       cout<<num[i];
}


output:

enter the value of n
4
enter numbers
1
5
4
3

ascending order is
1
3
4
5

No comments:

Post a Comment