/* * program to compute (real) roots of quadratic equation * * must compile with "-lm" flag to get sqrt() */ #include #include #include #include int main(void) { double a; double b; double c; double root1; double root2; double temp; printf("enter coefficients:\n"); if (scanf("%lf %lf %lf", &a, &b, &c) != 3) { printf("not numeric input\n"); exit(1); } if (a == 0) { if (b == 0) { printf("oh be real (a and b both zero is not sensible!)\n"); } else { root1 = -c / b; printf("root is %f\n", root1); } } else { temp = b*b - 4*a*c; if (temp < 0) { printf("no real roots\n"); } else { root1 = (-b + sqrt(temp)) / (2*a); root2 = (-b - sqrt(temp)) / (2*a); printf("roots are %f, %f\n", root1, root2); } } return 0; }