Basic calculator in C++

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this basic C++ program, we will have a look at how to code a basic calculator in C++.

C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch…case

# include <iostream>
using namespace std;
int main()
{
    char operator;
    float firstnumber, secondnumber;
    cout << "Please enter operator either + or - or * or /: ";
    cin >> operator;
    cout << "Please enter two operands: ";
    cin >> firstnumber>> secondnumber;
    switch(operator)
    {
        case '+':
            cout << firstnumber+secondnumber;
            break;
        case '-':
            cout << firstnumber-secondnumber;
            break;
        case '*':
            cout << firstnumber*secondnumber;
            break;
        case '/':
            cout << firstnumber/secondnumber;
            break;
        default:
            // If the operator is other than +, -, * or /, error message is shown
            cout << "Error! operator is not correct";
            break;
    }
    return 0;
}

Leave a Comment

Your email address will not be published. Required fields are marked *