-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCalculator.cpp
More file actions
44 lines (37 loc) · 1.03 KB
/
Calculator.cpp
File metadata and controls
44 lines (37 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
int main() {
char operation;
double num1, num2, result;
// Input
cout << "Enter an operation (+, -, *, /): ";
cin >> operation;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
// Perform the calculation
switch (operation) {
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 is not allowed." << endl;
return 1; // Exit with an error code
}
break;
default:
cout << "Invalid operation!" << endl;
return 1; // Exit with an error code
}
// Output
cout << "Result: " << num1 << " " << operation << " " << num2 << " = " << result << endl;
return 0; // Exit successfully
}