-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMini-Calculator.c
More file actions
60 lines (53 loc) · 1.83 KB
/
Mini-Calculator.c
File metadata and controls
60 lines (53 loc) · 1.83 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
// --------- Menu Based Mini Calculator in C ---------
#include <stdio.h>
int main() {
int choice;
double num1, num2, result;
while (1) {
// Display a menu to user again and again
printf("\n===== SIMPLE CALCULATOR MENU =====\n");
printf("1. Addition (+)\n");
printf("2. Subtraction (-)\n");
printf("3. Multiplication (*)\n");
printf("4. Division (/)\n");
printf("5. Exit\n");
printf("Choose an option (1-5): ");
scanf("%d", &choice);
// If user chooses Exit: thats why we added a condition
if (choice == 5) {
printf("Exiting calculator. Goodbye!\n");
break;
}
// Input numbers from user
printf("Enter first number: ");
scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);
// Perform a task for selected operation
switch (choice) {
case 1:
result = num1 + num2;
printf("Result: %.2lf + %.2lf = %.2lf\n", num1, num2, result);
break;
case 2:
result = num1 - num2;
printf("Result: %.2lf - %.2lf = %.2lf\n", num1, num2, result);
break;
case 3:
result = num1 * num2;
printf("Result: %.2lf * %.2lf = %.2lf\n", num1, num2, result);
break;
case 4:
if (num2 == 0) {
printf("Error: Division by zero is not allowed.\n");
} else {
result = num1 / num2;
printf("Result: %.2lf / %.2lf = %.2lf\n", num1, num2, result);
}
break;
default:
printf("Invalid option. Please choose from 1 to 5.\n");
}
}
return 0;
}