Answer to Question #225611 in Python for kaavya

Question #225611

Roots of a quadratic equation

You are given coefficients a, b and c of a quadratic equation ax2 + bx + c = 0. Find the roots r1, r2 of the equation.

Note:

r1 and r2 should be rounded up to 2 decimal places.Input

The first line of input is an integer a. The second line of input is an integer b. The third line of input is an integer c.

Explanation

In the given example, a = 1, b = -5, c = 6. Then the equation is x2 - 5x + 6 = 0


r1 = (-b + (b^2 - 4*a*c)^0.5)/2*a

r1 = (5 + (25 - 24))/2

r1 = 3


and


r2 = (-b - (b^2 - 4*a*c)^0.5)/2*a

r2 = (5 - (25 - 24))/2

r2 = 2


So, the output should be


3

2


Sample Input 1

1

-5

6

Sample Output 1

3.0

2.0

Sample Input 2

-1

1

6

Sample Output 2

-2.0

3.0




1
Expert's answer
2021-08-16T01:04:22-0400
a, b, c = [int(input()) for _ in range(3)]
if a == 0:
    print(round(-c / b, 2))
else:
    d = b * b - 4 * a * c
    if d < 0:
        print('no roots')
    elif d == 0:
        print(round(-b / 2 / a, 2))
    else:
        print(round((-b - d ** 0.5) / 2 / a, 2))
        print(round((-b + d ** 0.5) / 2 / a, 2))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS