#include <iostream>
using namespace std;
int main() {
double num1, num2;
char op;
cout << "🧮 Simple Calculator in C++\n";
cout << "---------------------------\n";
// Input from user
cout << "Enter first number: ";
cin >> num1;
cout << "Enter an operator (+, -, *, /): ";
cin >> op;
cout << "Enter second number: ";
cin >> num2;
cout << "---------------------------\n";
// Operation handling
switch(op) {
case '+':
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
if (num2 != 0)
cout << "Result: " << num1 / num2 << endl;
else
cout << "❌ Error: Division by zero!" << endl;
break;
default:
cout << "❌ Error: Invalid operator!" << endl;
}
return 0;
}
✅ Features:
- Supports:
+
,-
,*
,/
- Input validation for division by zero
- Clean and readable structure
🛠 How to Compile & Run:
- Save the code in a file, e.g.,
calculator.cpp
- Compile it with any C++ compiler:
g++ calculator.cpp -o calculator
- Run it:
./calculator
Would you like to extend this calculator with:
- multiple operations (chained expressions)?
- a GUI version (Qt or Windows)?
- scientific functions (sin, cos, sqrt, etc)?
Top comments (0)