Write a program that accepts two real numbers from a user and a select code. If the entered select code is 1, have the program add the two previously entered numbers and display the result; if the select code is 2, the numbers should be multiplied, and if the select code is 3, the first number should be divided by the second number. Division by 0 is not allowed and an appropriate message is displayed when such a division is attempted.
#include <iostream>
using namespace std;
int main()
{
/* Asks the user to enter 2 numbers */
double a, b;
cout << "Enter two real numbers: ";
cin >> a >> b;
/* Prompt for a select code */
int selectCode;
cout << "Enter a select code: ";
cin >> selectCode;
/* Perform an arithmetic operation according to the select code */
switch (selectCode)
{
case 1:
cout << "The sum is " << a + b << endl;
break;
case 2:
cout << "The product is " << a * b << endl;
break;
case 3:
if (b == 0)
cout << "Division by zero is not allowed" << endl;
else
cout << "The quotient is " << a / b << endl;
break;
default:
cout << "Wrong select command" << endl;
}
return 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!
Learn more about our help with Assignments:
C++