/* * Program to compute and print real roots of a*x**2 + b*x + c = 0 * (** denotes exponentiation) * * Version that puts code in a function and calls it with some tests / * examples first. */ #include #include void compute_roots(double a, double b, double c); int main(void) { compute_roots(1, -1, -6); compute_roots(1, 0, 4); compute_roots(0, 5, 2); compute_roots(0, 0, 0); compute_roots(0, 0, 5); printf("\nenter three coefficients\n"); double a, b, c; if (scanf("%lf %lf %lf", &a, &b, &c) != 3) { printf("need three numbers\n"); } compute_roots(a, b, c); return 0; } void compute_roots(double a, double b, double c) { double root1, root2; printf("coefficient a = %f, b = %f, c = %f\n", a, b, c); if (a == 0) { if (b == 0) { if (c != 0) { printf("no roots\n"); } else { printf("all x's are roots\n"); } } else { printf("one root %f\n", -c/b); } } else { double d = b*b - 4*a*c; if (d < 0) { printf("no real roots\n"); } else { root1 = (-b + sqrt(d)) / (2*a); root2 = (-b - sqrt(d)) / (2*a); printf("roots are %f, %f\n", root1, root2); } } }