Q: Write a program in C++ that will sort the array in ascending order
#include<iostream> using namespace std; int main() { int n; cout<<"how many numbers : ";cin>>n; int arr[n]; int num,temp=0; for(int i=0;i<n;i++) { cout<<"Enter number "<<i+1<<"\t"; cin>>arr[i]; } for(int i=0;i<n;i++) { for(int j=i;j<n-1;j++) { if(arr[i]>arr[j+1]) { temp=arr[i]; arr[i]=arr[j+1]; arr[j+1]=temp; } } }
cout<<"\n\t\tSorted Array\n"; for(int i=0;i<n;i++) { cout<<arr[i]<<"\t"; } cout<<"\n\n\n"; system("pause"); return 0; }
0 comments:
Post a Comment