Deprecated: Function Elementor\DB::is_built_with_elementor is deprecated since version 3.2.0! Use Plugin::\$instance->documents->get( \$post_id )->is_built_with_elementor() instead. in /home/u214178821/domains/sciedutut.com/public_html/wp-includes/functions.php on line 5383
Write python code for quadratic equation - Science Education and Tutorials

# 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.

``````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