-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask4.cpp
More file actions
198 lines (149 loc) · 5.12 KB
/
Copy pathtask4.cpp
File metadata and controls
198 lines (149 loc) · 5.12 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
// Display menu
void showMenu() {
cout << "\n========================================\n";
cout << " SCIENTIFIC CALCULATOR\n";
cout << "========================================\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Power (x^y)\n";
cout << "6. Square Root\n";
cout << "7. Sine\n";
cout << "8. Cosine\n";
cout << "9. Tangent\n";
cout << "10. Logarithm (base 10)\n";
cout << "11. Natural Logarithm (ln)\n";
cout << "12. Factorial\n";
cout << "13. Absolute Value\n";
cout << "14. Exit\n";
cout << "========================================\n";
}
// Factorial function
unsigned long long factorial(int n) {
unsigned long long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
// Convert degree to radians
double toRadians(double degree) {
return degree * acos(-1.0) / 180.0;
}
int main() {
int choice;
double a, b;
cout << fixed << setprecision(4);
do {
showMenu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Result = " << a + b << endl;
break;
case 2:
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Result = " << a - b << endl;
break;
case 3:
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Result = " << a * b << endl;
break;
case 4:
cout << "Enter two numbers: ";
cin >> a >> b;
if (b == 0) {
cout << "Error: Division by zero is not allowed.\n";
} else {
cout << "Result = " << a / b << endl;
}
break;
case 5:
cout << "Enter base and exponent: ";
cin >> a >> b;
cout << "Result = " << pow(a, b) << endl;
break;
case 6:
cout << "Enter a number: ";
cin >> a;
if (a < 0) {
cout << "Error: Square root of a negative number is not supported.\n";
} else {
cout << "Result = " << sqrt(a) << endl;
}
break;
case 7:
cout << "Enter angle in degrees: ";
cin >> a;
cout << "sin(" << a << ") = "
<< sin(toRadians(a)) << endl;
break;
case 8:
cout << "Enter angle in degrees: ";
cin >> a;
cout << "cos(" << a << ") = "
<< cos(toRadians(a)) << endl;
break;
case 9:
cout << "Enter angle in degrees: ";
cin >> a;
cout << "tan(" << a << ") = "
<< tan(toRadians(a)) << endl;
break;
case 10:
cout << "Enter a positive number: ";
cin >> a;
if (a <= 0) {
cout << "Error: Logarithm is defined only for positive numbers.\n";
} else {
cout << "log10(" << a << ") = "
<< log10(a) << endl;
}
break;
case 11:
cout << "Enter a positive number: ";
cin >> a;
if (a <= 0) {
cout << "Error: Natural logarithm is defined only for positive numbers.\n";
} else {
cout << "ln(" << a << ") = "
<< log(a) << endl;
}
break;
case 12: {
int n;
cout << "Enter a non-negative integer: ";
cin >> n;
if (n < 0) {
cout << "Error: Factorial is not defined for negative numbers.\n";
} else if (n > 20) {
cout << "Error: Please enter a number up to 20.\n";
} else {
cout << n << "! = " << factorial(n) << endl;
}
break;
}
case 13:
cout << "Enter a number: ";
cin >> a;
cout << "|" << a << "| = " << fabs(a) << endl;
break;
case 14:
cout << "\nThank you for using Scientific Calculator!\n";
break;
default:
cout << "\nInvalid choice! Please try again.\n";
}
} while (choice != 14);
return 0;
}