-
Notifications
You must be signed in to change notification settings - Fork 768
Expand file tree
/
Copy pathcalculator.c
More file actions
40 lines (34 loc) · 876 Bytes
/
calculator.c
File metadata and controls
40 lines (34 loc) · 876 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
#include <stdio.h>
void main(){
//declarations
double num1, num2;
char op;
printf("Enter the calculation:\n");
scanf("%lf %c %lf", &num1,&op,&num2);
switch(op){
case '+':
printf("%lf + %lf = %lf\n", num1, num2, num1+num2);
break;
case '-':
printf("%lf - %lf = %lf\n", num1, num2, num1-num2);
break;
case '*':
printf("%lf * %lf = %lf\n", num1, num2, num1*num2);
break;
case '/':
if(num2==0){
printf("Division by zero error!\n");
}else{
printf("%lf / %lf = %lf\n", num1, num2, num1/num2);
}
break;
case '%':
if((long)num2==0){
printf("Division by zero error!\n");
}else{
printf("%lf %% %lf = %ld\n", num1, num2, (long)num1%(long)num2);
}
break;
default: printf("Invalid Operator\n");
}
}