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.
1
Expert's answer
2013-03-15T10:05:42-0400
#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;
Comments
Leave a comment