/* 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 (s > 0) { x1 = (-(double)b - sqrt((double)s))/((double)(2*a)); x2 = (-(double)b + sqrt((double)s))/((double)(2*a)); printf("2 real solutions to %dx^2 +%dx +%d = 0.\n", a, b, c); printf("They are %lf and %lf.\n", x1, x2 ); return EXIT_SUCCESS; } else if (s == 0) { x1 = -((double)b)/((double)(2*a)); printf("1 real sol to %dx^2 +%dx +%d = 0.\n", a, b, c); printf("It is %lf.\n", x1); return EXIT_SUCCESS; } else { printf("No real sols to %dx^2 + %dx +%d = 0.\n", a, b, c); return EXIT_SUCCESS; } }