Need Help/Conditional Operator.

Q: Write a proogram that asks the user to enter 2 numbers. The program should use the CONDITIONAL OPERATOR to determine which number is the smaller and which is the larger.

I need the solution. Please help. I used this, which worked, but lost when it comes to CONDITIONAL OPERATOR.....

// Conditional Operator not used.
if (num1 > num2)
cout << num1 << " is the larger number" << endl;

else if (num2 > num1)
cout << num2 << " is the larger number" << endl;

else
cout << "both numbers are equal" <<endl;

Your solution is exactly right and uses the conditional operator >, although you didn't print the smaller number. You could add a second print to each if statement to do that.
Those are RELATIONAL OPERATORS.

Example of CONDITIONAL OPERATOR:

Ex 1: x < 0 ? y = 10 : z=20

Ex 2: hours = hours < 5 ? 5 : hours;

I'm studying "Starting out C++" (6ed) by Tony Gaddis.

No solution manual.

num1 != num2 ? num1 > num2 ? cout << "grater" : cout << "smaler" : cout << "equal";
or
cout << (a==b?"equal":(a>b)?"a is greater":"b is greater");
Topic archived. No new replies allowed.