(only by if-else and nested if- else)
Write a program that ask the users to enter three characters. Then the program should display the
following menu
1. Sort in Ascending order
2. Sort in Descending order
The program should perform sorting of these three characters in ascending or descending order as per the
user requirement.
using namespace std;
int main()
{
char a,b,c;
int Option=3;
while(Option)
{
Option = 4;
while(Option<0 || Option>2)
{
cout<<"\n\tPress 1 for ascending";
cout<<"\n\tPress 2 for descending";
cout<<"\n\tPress 0 to QUIT";
cout<<"\n\tEnter Option: "; cin>>Option;
}
if(Option==1 || Option==2)
{
cout<<"\n\tEnter numbers followed by space: ";
cin>>a>>b>>c;
if((a>=b)&&(a>=c))
{
if(b>=c)
{
if(Option==2) cout<<"\n Descending order : "<<a<<", "<<b<<", "<<c;
if(Option==1) cout<<"\n Ascending order : "<<c<<", "<<b<<", "<<a;
}
else
{
if(Option==2) cout<<"\n Descending order : "<<a<<", "<<c<<", "<<b;
if(Option==1) cout<<"\n Ascending order : "<<b<<", "<<c<<", "<<a;
}
}
else if((b>=a)&&(b>=c))
{
if(a>=c)
{
if(Option==2) cout<<"\n Descending order : "<<b<<", "<<a<<", "<<c;
if(Option==1) cout<<"\n Ascending order : "<<c<<", "<<a<<", "<<b;
}
else
{
if(Option==2) cout<<"\n Descending order : "<<b<<", "<<c<<", "<<a;
if(Option==1) cout<<"\n Ascending order : "<<a<<", "<<c<<", "<<b;
}
}
else if((c>=a)&&(c>=b))
{
if(a>=b)
{
if(Option==2) cout<<"\n Descending order : "<<c<<", "<<a<<", "<<b;
if(Option==1) cout<<"\n Ascending order : "<<b<<", "<<a<<", "<<c;
}
else
{
if(Option==2) cout<<"\n Descending order : "<<c<<", "<<b<<", "<<a;
if(Option==1) cout<<"\n Ascending order : "<<a<<", "<<b<<", "<<c;
}
}
}
else exit(0);
}
}
Comments
Leave a comment