The Roots of a Quadratic Equation
MediumRead the coefficients a, b, and c of ax^2 + bx + c = 0 as real numbers and print its roots. If a is zero (within a tolerance of 1e-9), print not a quadratic equation and return 1. Otherwise compute the discriminant d = b*b - 4ac and use an if, else if, else ladder with the same tolerance: when d is greater than 1e-9 print roots are real and distinct, then root1 = (-b + sqrt(d)) / 2a and root2 = (-b - sqrt(d)) / 2a on two lines; when d is within 1e-9 of zero print roots are real and equal, then root = -b / 2a; otherwise print roots are complex, then root1 = real + imagi and root2 = real - imagi, where real = -b / 2a and imag = sqrt(-d) / |2a|, so that imag is always positive and the sign printed between the parts is the sign of the root. Every value is printed to two decimal places in the forms root1 = 3.00, root = -1.00, root1 = -0.50 + 0.87i, and root2 = -0.50 - 0.87i. If the three numbers cannot be read, print invalid input and return 1.
Success Criteria
Your code must pass 8 test case(s) to complete this exercise. 3 hint(s) are available if you need help.
How did you find this exercise?
Your rating helps us improve the content.
Sign in to track your progress
You can work on exercises as a guest, but sign in to track your progress and save your submissions.