Summary: This programming tutorial explains the python program that solves the quadratic equation i.e. output the roots of the quadratic equation.
To solve a quadratic equation in Python, you can use the quadratic formula, which is:
x = [−b ± √(b2 − 4ac)]/2a
c are the coefficients of the quadratic equation in the form
ax2 + bx + c = 0.
Here is an example of a Python function that takes in the coefficients
c as arguments and returns the roots of the quadratic equation:
import cmath def solve_quadratic(a, b, c): # Calculate the discriminant discriminant = b**2 - 4*a*c # Calculate the two roots root1 = (-b + cmath.sqrt(discriminant)) / (2*a) root2 = (-b - cmath.sqrt(discriminant)) / (2*a) return root1, root2 # Test the function roots = solve_quadratic(1, 5, 6) print(roots)
This will output the roots of the quadratic equation
x2 + 5x + 6 = 0.