-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
64 lines (49 loc) · 1.34 KB
/
main.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
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, d, discriminant;
double tolerance = 0.001; // Could be a macro, not necessary given program size.
printf("Enter tolerance (e.g. 0.01): ");
if (scanf("%lf", &tolerance) != 1) {
printf("Invalid input!");
return 1;
}
printf("Enter the value for a, b, c and result...\n");
printf("a: ");
if (scanf("%lf", &a) != 1) {
printf("Invalid input!");
return 1;
}
printf("b: ");
if (scanf("%lf", &b) != 1) {
printf("Invalid input!");
return 1;
}
printf("c: ");
if (scanf("%lf", &c) != 1) {
printf("Invalid input!");
return 1;
}
printf("Equals to: ");
if (scanf("%lf", &d) != 1) {
printf("Invalid input!");
return 1;
}
c -= d;
d = 0.0;
discriminant = (b * b) - (4 * a * c); // Parentheses for clarity.
printf("Equation: %.2lfx^2 + %.2lfx + %.2lf = 0.00\n", a, b, c);
printf("Discriminant: %.2lf\n", discriminant);
double x1, x2;
if (discriminant > 0.0 + tolerance) { // 0.0 not really necessary.
x1 = (-b - sqrt(discriminant)) / (2 * a);
x2 = (-b + sqrt(discriminant)) / (2 * a);
printf("Solution: (%.2lf, %.2lf)\n", x1, x2);
} else if ((discriminant < 0.0 + tolerance) && (discriminant > 0.0 - tolerance)){
x1 = -b / (2 * a);
printf("Solution: %.2lf\n", x1);
} else if (discriminant < 0.0 - tolerance) {
printf("No solution!\n");
}
return 0;
}