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:
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.
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
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))
Comments
Leave a comment