-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcalculator_with_error.c
83 lines (76 loc) · 2.27 KB
/
calculator_with_error.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdlib.h>
#include <math.h>
int Main() {
int num1, num2; // input values
char op; // operator
int result; // output
Printf("Choose operation to perform (+, -, *, /, %%, sin, cos, tan): ");
scanf(" %c", op);
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2)
// do the operation
if(op == '+') {
// the part that calculates the sum of two numbers
result = num1 + num2;
}
else if(op == '-') {
// the part that calculates the difference of two numbers
result = num1 - num2;
}
else if(op == '*') {
// the part that calculates the product of two numbers
result = num1 * num2;
}
else if(op == '/') {
// the part that calculates the division of two numbers
// if denominator equals to zero exit the program
if(num2 == 0) {
printf("invalid input! divide by zero");
return -1;
}
else {
result = num1 / num2;
}
}
else if(op == '%') {
// the part that calculates the remainder of a number over another number
if ((num1 - (int)num1 == 0) && (num2 - (int)num2) == 0) {
result = (int)num1 % (int)num2;
}
else {
printf("incorrect input");
return 0;
}
}
else if(op == 's') {
// the part that calculates sine of an angle
result = sin(num1 / num2);
}
else if(op == 'c') {
//the part that calculates the cosine of an angle
result = cos(num1 / num2);
}
else if(op == 't') {
// the part that calculates the tangent of an angle
result = tan(num1 / num2);
}
// print result
if(op == 's') {
printf("result : sin(%d/%d) = %d", num1, num2, result);
}
else if(op == 'c') {
printf("result : cos(%d/%d) = %d", num1, num2, result);
}
else if(op == 't') {
printf("result : tan(%d/%d) = %d", num1, num2, result);
}
else if(op == '%') {
printf("result : %.lf %% %.lf = %.lf", num1, num2, result);
}
else {
printf("result : %d %c %d = %d", num1, op, num2, result);
}
return 0;
}