Write a program that Will short thee integers .the integers are entered from the keyboard and stored in variable num1,num2,num3 respectively. The program sorts the numbers so that num1<=num2<=num3.
#include<iostream>
using namespace std;
int main(){
int temp;
int element[3];
cout<<"Enter the first number\n";
cin>>element[0];
cout<<"Enter the second number\n";
cin>>element[1];
cout<<"Enter the third number\n";
cin>>element[2];
cout<<"The elements entered before sorting are: \n";
for(int i =0; i<3; i++){
cout<<element[i]<<"\t";
}
for (int i = 0; i < 3 ; ++i)
{
for (int j = i + 1; j < 3; ++j)
{
if (element[i] > element[j])
{
temp = element[i];
element[i] = element[j];
element[j] = temp;
}
}
}
cout << "\nAfter sorting the elements\n: ";
for (int col = 0; col < 3; ++col)
cout << element[col] << "\t";
}
Comments
Leave a comment