-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNext-level.c
191 lines (174 loc) · 5.29 KB
/
Next-level.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// Function prototypes
void printMenu();
double add(double a, double b);
double subtract(double a, double b);
double multiply(double a, double b);
double divide(double a, double b);
double exponentiate(double base, double exp);
double squareRoot(double a);
double logarithm(double a);
double sine(double a);
double cosine(double a);
double tangent(double a);
double factorial(int n);
int main() {
int choice;
double num1, num2, result;
int intNum;
while (1) {
printMenu();
printf("Enter your choice (1-12): ");
if (scanf("%d", &choice) != 1) {
printf("Invalid input! Please enter a number.\n");
while(getchar() != '\n'); // Clear input buffer
continue;
}
switch (choice) {
case 1:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = add(num1, num2);
printf("Result: %.2lf\n", result);
break;
case 2:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = subtract(num1, num2);
printf("Result: %.2lf\n", result);
break;
case 3:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = multiply(num1, num2);
printf("Result: %.2lf\n", result);
break;
case 4:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = divide(num1, num2);
if (num2 != 0) {
printf("Result: %.2lf\n", result);
}
break;
case 5:
printf("Enter the base and exponent: ");
scanf("%lf %lf", &num1, &num2);
result = exponentiate(num1, num2);
printf("Result: %.2lf\n", result);
break;
case 6:
printf("Enter a number: ");
scanf("%lf", &num1);
result = squareRoot(num1);
printf("Result: %.2lf\n", result);
break;
case 7:
printf("Enter a number: ");
scanf("%lf", &num1);
result = logarithm(num1);
printf("Result: %.2lf\n", result);
break;
case 8:
printf("Enter an angle in degrees: ");
scanf("%lf", &num1);
result = sine(num1);
printf("Result: %.2lf\n", result);
break;
case 9:
printf("Enter an angle in degrees: ");
scanf("%lf", &num1);
result = cosine(num1);
printf("Result: %.2lf\n", result);
break;
case 10:
printf("Enter an angle in degrees: ");
scanf("%lf", &num1);
result = tangent(num1);
printf("Result: %.2lf\n", result);
break;
case 11:
printf("Enter a non-negative integer: ");
scanf("%d", &intNum);
if (intNum < 0) {
printf("Error: Factorial is not defined for negative numbers.\n");
} else {
result = factorial(intNum);
printf("Result: %.0lf\n", result);
}
break;
case 12:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please choose a number between 1 and 12.\n");
break;
}
}
return 0;
}
void printMenu() {
printf("Advanced Calculator\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exponentiation\n");
printf("6. Square Root\n");
printf("7. Logarithm (base 10)\n");
printf("8. Sine (degrees)\n");
printf("9. Cosine (degrees)\n");
printf("10. Tangent (degrees)\n");
printf("11. Factorial\n");
printf("12. Exit\n");
}
double add(double a, double b) {
return a + b;
}
double subtract(double a, double b) {
return a - b;
}
double multiply(double a, double b) {
return a * b;
}
double divide(double a, double b) {
if (b == 0) {
printf("Error: Division by zero!\n");
return 0;
}
return a / b;
}
double exponentiate(double base, double exp) {
return pow(base, exp);
}
double squareRoot(double a) {
if (a < 0) {
printf("Error: Square root of a negative number is not real.\n");
return 0;
}
return sqrt(a);
}
double logarithm(double a) {
if (a <= 0) {
printf("Error: Logarithm of a non-positive number is undefined.\n");
return 0;
}
return log10(a);
}
double sine(double a) {
return sin(a * M_PI / 180); // Convert degrees to radians
}
double cosine(double a) {
return cos(a * M_PI / 180); // Convert degrees to radians
}
double tangent(double a) {
return tan(a * M_PI / 180); // Convert degrees to radians
}
double factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}