Python Program to Solve Quadratic Equation

Spread the love

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)

Ref: https://www.cliffsnotes.com/study-guides/algebra/algebra-i/quadratic-equations/solving-quadratic-equations

admin

admin

Leave a Reply

Your email address will not be published. Required fields are marked *