-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.c
More file actions
57 lines (55 loc) · 1.17 KB
/
Copy pathoutput.c
File metadata and controls
57 lines (55 loc) · 1.17 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
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdbool.h>
// Global variable declarations
double num1;
double num2;
double operation;
double result;
// Function definitions
double calculate(double a, double b, double op) {
if ((op == 1)) {
return (a + b);
}
else {
if ((op == 2)) {
return (a - b);
}
else {
if ((op == 3)) {
return (a * b);
}
else {
if ((op == 4)) {
return (a / b);
}
else {
return 0;
}
}
}
}
}
int main(void) {
num1 = 0;
num2 = 0;
operation = 0;
printf("Enter first number: \n");
if (scanf("%lf", &num1) != 1) {
num1 = 0;
scanf("%*s");
}
printf("Enter second number: \n");
if (scanf("%lf", &num2) != 1) {
num2 = 0;
scanf("%*s");
}
printf("Choose operation (1=+, 2=-, 3=*, 4=/): \n");
if (scanf("%lf", &operation) != 1) {
operation = 0;
scanf("%*s");
}
result = calculate(num1, num2, operation);
printf("Result: \n");
printf("%.2f\n", result);
return 0;
}