-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.c
More file actions
45 lines (40 loc) · 1.02 KB
/
calculator.c
File metadata and controls
45 lines (40 loc) · 1.02 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
#include <stdio.h>
int main(){
// declaration of variable section
char operator;
double num1;
double num2;
double result;
do {
printf(">>>>Enter one of these operators (+, -, /, *): ");
scanf(" %c", &operator);
if (operator != '+' && operator != '-' && operator != '*' && operator != '/') {
printf(">>>>Invalid operator, Pls try again!\n");
}
} while (operator != '+' && operator != '-' && operator != '*' && operator != '/');
printf(">>enter the first number: ");
scanf("%lf",&num1);
printf(">>enter the second number: ");
scanf("%lf",&num2);
switch (operator)
{
case '+':
result =num1 + num2 ;
break;
case '/':
result =num1 / num2 ;
break;
case '-':
result =num1 - num2 ;
break;
case '*':
result =num1 * num2 ;
break;
default:
printf("unexpected error \n");
return 1;
}
// print the result
printf("<------the result is %.2lf------>", result);
return 0;
}