-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.c
More file actions
43 lines (39 loc) · 940 Bytes
/
Copy pathCalculator.c
File metadata and controls
43 lines (39 loc) · 940 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
38
39
40
41
42
43
// Online C compiler to run C program online
#include <stdio.h>
int main() {
char ch; float a, b, sum;
printf("Enter an operator (+, -, *, /):");
scanf("%c", &ch);
printf("Enter 1st no.: ");
scanf("%f", &a);
printf("Enter 2nd no.: ");
scanf("%f", &b);
switch(ch){
case '+':
sum = a + b;
printf("%.2f + %.2f = %.2f\n", a, b, sum);
break;
case'-':
sum = a - b;
printf("%.2f - %.2f = %.2f\n", a, b, sum);
break;
case'*':
sum = a * b;
printf("%.2f * %.2f = %.2f\n", a, b, sum);
break;
case'/':
if (a || b != 0)
{
sum = a / b;
printf("%.2f / %.2f = %.2f\n", a, b, sum);
}
else
{
printf("Error! Division by zero.\n");
}
break;
default:
printf("Invalid operator!\n");
}
return 0;
}