Implement the function find_roots to find the roots of the quadratic equation:= ax2 + bx + c = 0. the function should return a tuple containing roots in any order. If the equation has only one solution. The Equation will always have at least one solution.
The roots of the quadratic equation can be found with the following formula :
-b = +- square_root b2 - 4ac
for example, find _roots (2, 10, 8) should return (-1, -4) or (-4, -1) as the roots of the equation 2x2 + 10X + 8 are -1 and -4
"\\lnot"
import math
a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))
def find_roots(a, b, c):
D = b ** 2 - 4 * a * c
if D > 0:
x1 = round(((-b + math.sqrt(D)) / (2 * a)), 2)
x2 = round(((-b - math.sqrt(D)) / (2 * a)), 2)
print((x1, x2))
elif D == 0:
x1 = x2 = -b / 2 * a
print((x1))
else:
print('The equation has no solution')
find_roots(a, b, c)
Comments
Leave a comment