-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.cpp
More file actions
97 lines (90 loc) · 2.94 KB
/
Copy pathCalculator.cpp
File metadata and controls
97 lines (90 loc) · 2.94 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <cmath>
using namespace std;
// Function to calculate factorial
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
char operation;
double num1, num2;
cout << "Complex Calculator" << endl;
cout << "------------------" << endl;
// Input the operation
cout << "Choose an operation:" << endl;
cout << "1. Addition (+)" << endl;
cout << "2. Subtraction (-)" << endl;
cout << "3. Multiplication (*)" << endl;
cout << "4. Division (/)" << endl;
cout << "5. Exponentiation (^)" << endl;
cout << "6. Square root (sqrt)" << endl;
cout << "7. Factorial (!)" << endl;
cout << "Enter the operation: ";
cin >> operation;
// Perform the operation based on user's choice
switch (operation) {
case '+':
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Result: " << num1 + num2 << endl;
break;
case '-':
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Result: " << num1 - num2 << endl;
break;
case '*':
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
cout << "Result: " << num1 * num2 << endl;
break;
case '/':
cout << "Enter dividend: ";
cin >> num1;
cout << "Enter divisor: ";
cin >> num2;
if (num2 != 0) {
cout << "Result: " << num1 / num2 << endl;
} else {
cout << "Error: Division by zero is not allowed!" << endl;
}
break;
case '^':
cout << "Enter base: ";
cin >> num1;
cout << "Enter exponent: ";
cin >> num2;
cout << "Result: " << pow(num1, num2) << endl;
break;
case 'sqrt':
cout << "Enter number: ";
cin >> num1;
if (num1 >= 0) {
cout << "Result: " << sqrt(num1) << endl;
} else {
cout << "Error: Cannot calculate square root of a negative number!" << endl;
}
break;
case '!':
cout << "Enter a non-negative integer: ";
cin >> num1;
if (num1 >= 0 && num1 == int(num1)) {
cout << "Result: " << factorial(num1) << endl;
} else {
cout << "Error: Factorial is defined only for non-negative integers!" << endl;
}
break;
default:
cout << "Error: Invalid operation!" << endl;
}
return 0;
}