How to find the roots of a quadratic equation in Python

1 Answer

0 votes
import cmath

def quadratic_equation_roots(a, b, c):
    discriminant = (b**2) - (4*a*c)

    root1 = (-b - cmath.sqrt(discriminant)) / (2 * a)
    root2 = (-b + cmath.sqrt(discriminant)) / (2 * a)

    print("root1 = ", root1, "\nroot2 = ", root2);


a = 3
b = 5
c = -9
quadratic_equation_roots(a, b, c)
print()

a = 3
b = 5
c = 7
quadratic_equation_roots(a, b, c)
print()

a = 2
b = 4
c = 2
quadratic_equation_roots(a, b, c)




'''
run:

root1 =  (-2.7554270991117993+0j) 
root2 =  (1.0887604324451328+0j)

root1 =  (-0.8333333333333334-1.2801909579781012j) 
root2 = (-0.8333333333333334+1.2801909579781012j)

root1 =  (-1+0j) 
root2 =  (-1+0j)

'''

 



answered Jan 11, 2022 by avibootz

Related questions

1 answer 232 views
1 answer 223 views
1 answer 201 views
1 answer 236 views
1 answer 150 views
1 answer 213 views
1 answer 221 views
...