/** * quad.c - Use the quadratic formula! */ /* import the standard input-output library */ #include /* import the math library */ #include /* define the main method */ int main(void) { /* define the variables we will use */ /* ax^2 + bx + c = 0 */ double a; double b; double c; /* prompt for an input of a */ printf("Enter coefficient for x^2: "); /* input the value for a */ scanf("%lf", &a); /* doubles need the %lf placeholder! */ /* prompt for an input of b */ printf("Enter coefficient for x: "); /* input the value for b */ scanf("%lf", &b); /* prompt for an input of c */ printf("Enter constant: "); /* input the value for c */ scanf("%lf", &c); /* use these values to calculate portions of the quadratic formula */ /* b^2 - 4ac */ double discriminant = b*b - 4.0L * a * c; /* sqrt(b^2 - 4ac) */ double disc_sqrt = sqrt(discriminant); /* first root, with a + */ double root1 = (-b + disc_sqrt)/(2.0L * a); /* second root, with a - */ double root2 = (-b - disc_sqrt)/(2.0L * a); /* output the roots */ printf("Roots are %lf and %lf.\n", root1, root2); /* included \n since not expecting input */ /* program over, return 0 for no errors! */ return 0; } /* main method over, done! */