C++ Program to Make a Simple Calculator

Last Updated : 31 Mar, 2026

A simple calculator is a device used to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. It makes arithmetic calculations easier and faster. In this article, we will learn how to code a simple calculator using C++.

Example:

Input: Enter an operator (+, -, *, /): *
Enter two numbers: 10 5
Output: Result: 50
Explanation: Chosen operation is multiplication, so 10 * 5 = 50

There are two different ways to make simple calculator program in C++:

1. Using Switch Statement

In C++, the switch statement allows us to define different cases that will be executed based on the value of a given variable. We can use it to define four cases for addition, subtraction, multiplication, and division which will be executed based on the value that user input to select the operation type.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    char op;
    double a, b, res;

    cout << "Enter an operator (+, -, *, /): ";
    cin >> op;
    cout << "Enter two numbers: ";
    cin >> a >> b;

    switch (op) {
    case '+':
        res = a + b;
        break;
    case '-':
        res = a - b;
        break;
    case '*':
        res = a * b;
        break;
    case '/':
        res = a / b;
        break;
    default:
        cout << "Error! Operator is not correct";
        res = -DBL_MAX;
    }

    if (res != -DBL_MAX)
        cout << "Result: " << res;
    return 0;
}

Output

Enter an operator (+, -, *, /): *
Enter two numbers: 10 5
Result: 50

Note: The break statement prevents fall-through by stopping execution from continuing into the next case after a match is found. Otherwise, all the cases after the matching case will be executed.

2. Using if-else Statement

In C++, a simple calculator can be implemented using an if–else if ladder by evaluating the user’s selected operator and executing the corresponding arithmetic operation based on matching conditions.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    char op;
    double a, b, res;

    cout << "Enter an operator (+, -, *, /): ";
    cin >> op;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    if (op == '+')
        res = a + b;
    else if (op == '-')
        res = a - b;
    else if (op == '*')
        res = a * b;
    else if (op == '/')
        res = a / b;
    else {
        cout << "Error! Operator is not correct";
        res = -DBL_MAX;
    }

    if (res != -DBL_MAX)
        cout << "Result: " << res;
    return 0;
}

Output

Enter an operator (+, -, *, /): *
Enter two numbers: 10 5
Result: 50

Comment