Answer to Question #325679 in C++ for SAMO

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


1
Expert's answer
2022-04-08T01:50:22-0400
#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS