/* Compute the two roots of a quadratic. */ #include #include #include // Need to include math.h to use sqrt. int main(void) { /* Variables for the three co-efficients of the quadratic equation, and also for the two roots we will find. */ int a, b, c, s; double x1, x2; printf("Input the x^2 co-efficient a: "); scanf("%d", &a); printf("Input the x co-efficient b: "); scanf("%d", &b); printf("Input the constant term c: "); scanf("%d", &c); s = (b*b -4*a*c); if (a != 0) { if (s < 0) { printf("No real roots to this quadratic.\n"); } else if (s == 0) { printf("Eq. has the repeated root %f.\n", -((double)b)/(2.0*a)); } else { x1 = (-(double)b - sqrt(s))/(2.0*a); x2 = (-(double)b + sqrt(s))/(2.0*a); printf("The sols to %dx^2 +%dx +%d = 0 are ", a, b, c); printf("%lf and %lf.\n", x1, x2 ); } } else if (b != 0) { x1 = -((double)c)/((double)(b)); printf("1 sol to %dx^2 +%dx +%d = 0.\n", a, b, c); printf("It is %lf.\n", x1); } else if (c != 0) { /* a AND b BOTH ZERO, c NON-ZERO */ printf("No sols to %dx^2 + %dx +%d = 0.\n", a, b, c); } else { /* a, b, c ALL ZERO */ printf("Degenerate equation - everything is a solution!\n"); } return EXIT_SUCCESS; }