Write python code for quadratic equation

This code defines a function solve_quadratic(a, b, c) which takes in the coefficients of a quadratic equation (ax^2 + bx + c = 0) and returns the two solutions. The solutions are calculated using the quadratic formula (-b ± √(b^2 – 4ac)) / 2a. The cmath library is used to calculate the square root of the discriminant (b^2 – 4ac) so that the solutions can be returned as complex numbers. The function is then called with example values for a, b, and c, and the solutions are printed.

Solving Quadratic Equations in Python
import cmath

def solve_quadratic(a, b, c):
    # calculate the discriminant
    disc = cmath.sqrt(b**2 - 4*a*c)
    # find two solutions
    sol1 = (-b + disc) / (2*a)
    sol2 = (-b - disc) / (2*a)
    return sol1, sol2

a = 1
b = 5
c = 6
print(solve_quadratic(a, b, c))

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Is HBr polar or nonpolar Is HCl polar or nonpolar Is NO2+ Polar or Nonpolar Is H2S Polar or Nonpolar Is PCl3 Polar or Nonpolar