-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimple_examples.c
More file actions
44 lines (41 loc) · 1.03 KB
/
simple_examples.c
File metadata and controls
44 lines (41 loc) · 1.03 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
/* function to add two numbers */
int add(int a, int b)
{
return a + b;
}
/* function to add, subtract, multiply and divide two numbers based on an operator */
int calculate(int a, int b, char op)
{
int result = 0;
switch (op)
{
case '+':
result = add(a, b);
break;
case '-':
result = subtract(a, b); /* Commentary: not sure where subtract is defined */
break;
case '*':
result = multiply(a, b); /* Commentary: not sure where multiply is defined */
break;
case '/':
result = divide(a, b); /* Commentary: not sure where divide is defined */
break;
default:
printf("Invalid operator");
}
return result;
}
/* main function */
int main()
{
int a, b, result;
char op;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Enter an operator: ");
scanf(" %c", &op);
result = calculate(a, b, op);
printf("Result: %d", result);
return 0;
}