-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_calculator.c
42 lines (36 loc) · 1.07 KB
/
basic_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
#include <stdio.h>
int main() {
char opt;
int number1, number2;
float result;
printf("Choose one of those operators:\n +, /, *, -\n Enter your choice: ");
scanf(" %c", &opt);
printf("Done, The operator is %c\n", opt);
printf("Now...\nEnter the first number: ");
scanf("%d", &number1);
printf("Cool, the first number is %d.\nNow enter the second number: ", number1);
scanf("%d", &number2);
switch (opt) {
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
if (number2 == 0) {
printf("ERROR! You can't divide by zero!!!\n");
return 1;
}
result = (float)number1 / number2;
break;
default:
printf("Invalid operator chosen!\n");
return 1;
}
printf("Result: %f\n", result);
return 0;
}