Answer to Question #266076 in C++ for DANISH

Question #266076

(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.


1
Expert's answer
2021-11-15T00:23:02-0500
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);
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS