Question #325679

Write a program that mimics a calculator. The program should take as input two integers and the operation

to be performed.

It should then output the numbers, the operator, and the result. (For division, if the denominator is zero,

output an appropriate message.)

Some sample outputs are as follow:

3 + 4 = 7

13 * 5 = 65


Expert's answer

#include <iostream>
using namespace std;


int main() {
    double x, y, z;
    char op;


    cout << "Enter the first number: ";
    cin >> x;
    cout << "Enter the second number: ";
    cin >> y;
    cout << "Enter an operator: ";
    cin >> op;


    switch (op) {
        case '+':
            z = x + y;
            break;
        case '-':
            z = x - y;
            break;
        case '*':
            z = x * y;
            break;
        case '/':
            if (y == 0) {
                cout << "Denominator is zero";
                return 1;
            }
            z = x / y;
            break;
        default:
            cout << "Unknown operator";
            return 1;
    }
    cout << x << ' ' << op << ' ' << y << " = " << z << 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!

LATEST TUTORIALS
APPROVED BY CLIENTS