Friday, 17 April 2015

C++ Program for Sorting the Number in Ascending and Descending Order

AIM
Write and execute a C++ program to sort the numbers 25,127,2565,3,9367,-563,25,12,-100,5,432 and 37 in ascending and descending  order.

PROGRAM

#include<iostream>
using namespace std;
int main()
{
int a[50],i,j,n,temp;
cout<<"Enter how many numbers to be sorted:";
cin>>n;
cout<<"Enter the elements:";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"\n The elements in ascending order:";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<"\n The elements in descending order:";
for(i=n-1;i>=0;i--)
cout<<a[i]<<"\t";
cout<<"\n";
return(0);
}



OUTPUT

Enter how many numbers to be sorted:12
Enter the elements:25
127
2565
3
9327
-563
25
12
-100
5
432
37

 The elements in ascending order:-563 -100 3    5    12   25   25   37   127  432     2565 9327

 The elements in descending order:9327     2565 432  127  37   25   25   12   5     3    -100 -563

No comments :

Post a Comment

Related Posts Plugin for WordPress, Blogger...