91.8k views
2 votes
Making Decisions Programming Challenges 1. Minimum/Maximum Write a program that asks the user to enter two numbers. The program should use the conditional operator to determine which number is the smaller and which is the larger?

User Tahlil
by
5.5k points

1 Answer

1 vote

Answer:

int main()

{

int a,b,max,min;

cout<<"Enter 2 numbers\\";

cin>>a>>b; //asking numbers from user

max= a>b ? a : b; //using conditional operator to find larger

min= a>b ? b : a; //using conditional operator to find smaller

cout<<"\\Larger is "<<max;

cout<<"\\Smaller is "<<min;

return 0;

}

OUTPUT :

Enter 2 numbers

56

34

Larger is 56

Smaller is 34

Step-by-step explanation:

  • In the above program, first user is asked to enter 2 numbers which needs to be compared.
  • Conditional operator - This operator contains syntax - (condition ? true : false) . This is a ternary operator in which first operand checks the condition and if its true second operand is returned otherwise third is returned.
  • Then conditional operator is used twice first to find larger number and then smaller.
  • At last both numbers are printed.
User Amanda Kitson
by
5.7k points