-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic Calculator
More file actions
37 lines (31 loc) · 804 Bytes
/
Basic Calculator
File metadata and controls
37 lines (31 loc) · 804 Bytes
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
#include <stdio.h>
int main() {
int num1, num2;
char operator;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter the second number: ");
scanf("%d", &num2);
if (operator == '+') {
printf("Result: %d\n", num1 + num2);
}
else if (operator == '-') {
printf("Result: %d\n", num1 - num2);
}
else if (operator == '*') {
printf("Result: %d\n", num1 * num2);
}
else if (operator == '/') {
if (num2 != 0) {
printf("Result: %.2f\n", (float) num1 / num2);
} else {
printf("Error: Division by zero\n");
}
}
else {
printf("Error: Invalid operator\n");
}
return 0;
}