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?

Respuesta :

Answer:

int main()

{

   int a,b,max,min;

   cout<<"Enter 2 numbers\n";

   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<<"\nLarger is "<<max;

   cout<<"\nSmaller is "<<min;

   return 0;

}

OUTPUT :

Enter 2 numbers

56

34

Larger is 56

Smaller is 34

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.