Here is a Python program that can be used to solve a quadratic equation:
import math def solve_quadratic_equation(a, b, c): # calculate the discriminant d = (b**2) - (4*a*c) # check if the discriminant is negative if d < 0: print("This equation has no real solutions") elif d == 0: # calculate the only solution x = (-b + math.sqrt(d)) / (2*a) print("The only solution is: ", x) else: # calculate the two solutions x1 = (-b + math.sqrt(d)) / (2*a) x2 = (-b - math.sqrt(d)) / (2*a) print("The solutions are: ", x1, "and", x2) # test the function solve_quadratic_equation(1, 3, 2)
To use this program, simply call the
solve_quadratic_equation() function with the coefficients of the quadratic equation as arguments. For example, to solve the equation
x^2 + 3x + 2 = 0, you would call the function like this:
solve_quadratic_equation(1, 3, 2)
The program will then calculate the solutions to the equation, if any, and print them to the screen.
Thanks for reading. Happy coding!