-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp
More file actions
43 lines (36 loc) · 1.32 KB
/
Copy pathcpp
File metadata and controls
43 lines (36 loc) · 1.32 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
#include <iostream>
#include <iomanip> // For setprecision
int main() {
char op;
float num1, num2;
char choice;
do {
std::cout << "Enter operator (+, -, *, /): ";
std::cin >> op;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
std::cout << std::fixed << std::setprecision(2); // Set precision for floating point output
switch(op) {
case '+':
std::cout << num1 << " + " << num2 << " = " << num1 + num2 << std::endl;
break;
case '-':
std::cout << num1 << " - " << num2 << " = " << num1 - num2 << std::endl;
break;
case '*':
std::cout << num1 << " * " << num2 << " = " << num1 * num2 << std::endl;
break;
case '/':
if(num2 != 0.0)
std::cout << num1 << " / " << num2 << " = " << num1 / num2 << std::endl;
else
std::cout << "Division by zero is not allowed." << std::endl;
break;
default:
std::cout << "Invalid operator!" << std::endl;
}
std::cout << "Do you want to perform another calculation? (y/n): ";
std::cin >> choice;
} while(choice == 'y' || choice == 'Y');
return 0;
}