Python Program to Solve Quadratic Equation
In this tutorial, you will learn how to write Python Program to Solve Quadratic Equation.
Equation:
ax 2 + bx + c = 0
Program:
For this program, we need to import the cmath library. The final output variables are in complex numbers. first we need initialize the variables and calculate the discriminant value.
# import complex math module
import cmath
a = 1
b = 2
c = 3
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
sol1 = (-b-cmath.sqrt(d))/(2*a)
sol2 = (-b+cmath.sqrt(d))/(2*a)
print('quadratic equation solution 1 ', sol2)
print('quadratic equation solution 2 ', sol2)
Output:
quadratic equation solution 1 (-1+1.4142135623730951j) quadratic equation solution 2 (-1+1.4142135623730951j)