-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.c
More file actions
66 lines (60 loc) · 1.55 KB
/
Copy pathcalculator.c
File metadata and controls
66 lines (60 loc) · 1.55 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
#include <stdio.h>
void add() {
double num1, num2;
printf("Enter two numbers to add: ");
scanf("%lf %lf", &num1, &num2);
printf("Result: %lf\n", num1 + num2);
}
void subtract() {
double num1, num2;
printf("Enter two numbers to subtract: ");
scanf("%lf %lf", &num1, &num2);
printf("Result: %lf\n", num1 - num2);
}
void multiply() {
double num1, num2;
printf("Enter two numbers to multiply: ");
scanf("%lf %lf", &num1, &num2);
printf("Result: %lf\n", num1 * num2);
}
void divide() {
double num1, num2;
printf("Enter two numbers to divide: ");
scanf("%lf %lf", &num1, &num2);
if (num2 != 0)
printf("Result: %lf\n", num1 / num2);
else
printf("Error: Division by zero is not allowed.\n");
}
int main() {
int choice;
while (1) {
printf("\nSimple Calculator:\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add();
break;
case 2:
subtract();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
}