-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.c
44 lines (31 loc) · 825 Bytes
/
Calculator.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
#include <stdio.h>
int main(){
float a, b, result;
char operator;
printf("Enter two numbers: \n");
scanf("%f %f", &a, &b);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator); // Added a space before %c to consume the newline character
switch (operator){
case '+': result = a + b;
break;
case '-': result = a - b;
break;
case '*': result = a * b;
break;
case '/':
if( b != 0){
result = a / b;
} else {
printf("Error: Division by zero\n");
break;
}
break;
default:
printf("Invalid operator\n");
return 1;
}
// Printing result
printf("Result: %f\n", result);
return 0;
}