/* * Program to calculate roots of a quadratic equation using * the quadratic formula. For now, input is hard-coded. */ #include #include void solve_and_print(double a, double b, double c); int main(void) { solve_and_print(1, 0, -4); solve_and_print(1, 0, 4); solve_and_print(1, 0, 0); solve_and_print(0, 1, 4); solve_and_print(0, 0, 4); solve_and_print(0, 0, 0); return 0; } /* print root(s) of a*x*x + b*x + c = 0, or message if none */ void solve_and_print(double a, double b, double c) { double discrim; printf("for equation with coefficients %g, %g, %g:\n", a, b, c); if (a == 0) { if (b != 0) { printf("one root: %g\n", c/b); } else if (c != 0) { printf("no roots\n"); } else { printf("all numbers are roots\n"); } } else { discrim = b*b - 4*a*c; if (discrim > 0) { printf("two roots: %g %g\n", (-b + sqrt(discrim))/(2*a), (-b - sqrt(discrim))/(2*a)); } else if (discrim == 0) { printf("one root: %g\n", (-b) / (2*a)); } else { printf("no roots\n"); } } }