C++ Program to Create Simple Calculator
A simple calculator in C++ can be created by taking two numbers and an operator as input, then using
switch to perform +, -, *, or / operations, like: switch(op) { case '+': result = num1 + num2; break; ... }.Examples
Input5 + 3
OutputResult: 8
Input10 / 2
OutputResult: 5
Input7 * 0
OutputResult: 0
How to Think About It
To build a simple calculator, first get two numbers and an operator from the user. Then check which operator was entered using
switch or if statements. Perform the matching arithmetic operation and show the result. Handle division carefully to avoid dividing by zero.Algorithm
1
Get the first number from the user2
Get the operator (+, -, *, /) from the user3
Get the second number from the user4
Use a switch or if-else to check the operator5
Perform the corresponding arithmetic operation6
Display the result to the userCode
cpp
#include <iostream> using namespace std; int main() { double num1, num2, result; char op; cout << "Enter first number: "; cin >> num1; cout << "Enter operator (+, -, *, /): "; cin >> op; cout << "Enter second number: "; cin >> num2; switch(op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 != 0) result = num1 / num2; else { cout << "Error: Division by zero!" << endl; return 1; } break; default: cout << "Invalid operator" << endl; return 1; } cout << "Result: " << result << endl; return 0; }
Output
Enter first number: 5
Enter operator (+, -, *, /): +
Enter second number: 3
Result: 8
Dry Run
Let's trace the input '5 + 3' through the code
1
Input first number
num1 = 5
2
Input operator
op = '+'
3
Input second number
num2 = 3
4
Check operator and calculate
op is '+', so result = 5 + 3 = 8
5
Output result
Print 'Result: 8'
| Step | Variable | Value |
|---|---|---|
| 1 | num1 | 5 |
| 2 | op | + |
| 3 | num2 | 3 |
| 4 | result | 8 |
Why This Works
Step 1: Input collection
The program asks the user for two numbers and an operator using cin to read input.
Step 2: Operator selection
The switch statement checks the operator and selects the matching arithmetic operation.
Step 3: Calculation and output
The program performs the calculation and prints the result with cout. It also handles division by zero to avoid errors.
Alternative Approaches
Using if-else instead of switch
cpp
#include <iostream> using namespace std; int main() { double num1, num2, result; char op; cout << "Enter first number: "; cin >> num1; cout << "Enter operator (+, -, *, /): "; cin >> op; cout << "Enter second number: "; cin >> num2; if (op == '+') result = num1 + num2; else if (op == '-') result = num1 - num2; else if (op == '*') result = num1 * num2; else if (op == '/') { if (num2 != 0) result = num1 / num2; else { cout << "Error: Division by zero!" << endl; return 1; } } else { cout << "Invalid operator" << endl; return 1; } cout << "Result: " << result << endl; return 0; }
This approach uses if-else statements which some find easier to read but can be longer for many cases.
Using functions for each operation
cpp
#include <iostream>
#include <stdexcept>
using namespace std;
double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
double multiply(double a, double b) { return a * b; }
double divide(double a, double b) {
if (b == 0) throw runtime_error("Division by zero");
return a / b;
}
int main() {
double num1, num2, result;
char op;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter second number: ";
cin >> num2;
try {
switch(op) {
case '+': result = add(num1, num2); break;
case '-': result = subtract(num1, num2); break;
case '*': result = multiply(num1, num2); break;
case '/': result = divide(num1, num2); break;
default:
cout << "Invalid operator" << endl;
return 1;
}
cout << "Result: " << result << endl;
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
return 1;
}
return 0;
}This approach organizes code better by separating operations into functions and uses exception handling for errors.
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a fixed amount of memory for variables and no extra data structures, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time; using functions adds slight overhead but improves readability and maintainability.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Switch statement | O(1) | O(1) | Simple and clear operator selection |
| If-else statements | O(1) | O(1) | Easier for beginners with few operators |
| Functions with exception handling | O(1) | O(1) | Better code organization and error handling |
Always check for division by zero before dividing to avoid runtime errors.
Beginners often forget to handle division by zero, causing the program to crash or give wrong results.