Python Program to Solve Quadratic Equations – 2022

Machine Learning Projects

Hey guys, in this blog we will see a Python Program to Solve Quadratic Equations using the cmath library.

Code

# Python Program to Solve Quadratic Equations

# Solve the quadratic equation ax**2 + bx + c = 0

import cmath

a = 1
b = 5
c = 6

# calculate the discriminant
determinant = (b**2) - (4*a*c)
root_of_determinant = cmath.sqrt(determinant)

# let's find two solutions
root1 = (-b-root_of_determinant)/(2*a)
root2 = (-b+root_of_determinant)/(2*a)

print(f'The solution are {root1} and {root2}')

Output

The solution are (-3+0j) and (-2+0j)

Here we have used the cmath library to find the square root of our Determinant.

We can also say that the roots are -3 and -2 because the imaginary part is 0 in both the roots.

Check out our other python programming examples

Leave a Comment

Your email address will not be published.