/* * Program to find roots of a quadratic equation. * No input for now -- input is hardcoded. */ #include #include void find_roots(double a, double b, double c); int main(void) { find_roots(1.0, 0.0, 0.0); find_roots(1.0, 0.0, -1.0); find_roots(1.0, 0.0, 1.0); find_roots(1.0, -5.0, 6.0); /* (x - 2)(x - 3) */ find_roots(2.0, -7.0, 3.0); /* (2x - 1)(x - 3) */ find_roots(0.0, 1.0, 2.0); find_roots(0.0, 0.0, 1.0); find_roots(0.0, 0.0, 0.0); return 0; } /* print root(s) of a*x*x + b*x + c = 0, or message if none */ void find_roots(double a, double b, double c) { double discriminant = b*b - 4*a*c; printf("\na = %g, b = %g, c = %g\n", a, b, c); if (a == 0) { if (b == 0) { if (c == 0) { printf("everything is an answer!\n"); } else { printf("there are no answers\n"); } } else { printf("the answer is %g\n", -c / b); } } else if (discriminant > 0) { printf("the answers are %g %g\n", (-b + sqrt(discriminant)) / (2*a), (-b - sqrt(discriminant)) / (2*a)); } else if (discriminant == 0) { printf("the answer is %g\n", -b / (2*a)); } else { printf("there are no answers\n"); } }