/* * Program to calculate solutions (roots) of a quadratic equation * * a*x*x + b*x + c = 0 * * using the quadratic formula. * * Input: Floating-point numbers a, b, and c (coefficients of * equation). * * Output: Solution(s) of the equation, if they exist, or a message saying * that there are none. */ #include #include /* needed for sqrt() function */ int main(void) { float a; float b; float c; float discrim; /* discriminant -- b*b - 4*a*c */ printf("enter coefficients (a, b, c):\n"); scanf("%f", &a); scanf("%f", &b); scanf("%f", &c); printf("Solutions for %g * x*x + %g * x + %g = 0:\n", a, b, c); /* first check for a == 0 */ if (a == 0) { if (b == 0) { if (c == 0) { printf("(any number)\n"); } else { printf("(none)\n"); } } else { printf("%g\n", -c / b); } } else { /* compute discriminant */ discrim = (b*b) - (4*a*c); /* if negative, no solutions */ if (discrim < 0) { printf("(none)\n"); } /* if zero, one solution */ else if (discrim == 0) { printf("%g\n", -b / (2*a) ); } /* if positive, two answers */ else { /* if we get here, we know discrim > 0 */ printf("%g\n", (-b + sqrt(discrim)) / (2 * a) ); printf("%g\n", (-b - sqrt(discrim)) / (2 * a) ); } } return 0; }