Learn Before
Code

Python Program to Solve Quadratic Equations

The standard form of a quadratic equation is:

ax^2 + bx + c = 0

where a, b, and c are real numbers and a ≠ 0

The solutions of this quadratic equation is given by:

(-b ± (b^2 - 4 * a * c)^0.5) / (2 * a)

Source Code

#Solve the quadratic equation ax^2 + bx + c = 0

#Import complex math module

import cmath

#Define variables

a = float (input ("Enter the value of a: "))

b = float (input ("Enter the value of b: "))

c = float (input ("Enter the value of c: "))

#Calculate the discriminant

d = (b**2) - (4ac)

#Solve for the two solutions

sol1 = (-b - cmath.sqrt(d))/(2*a)

sol2 = (-b + cmath.sqrt(d))/(2*a)

print ("The solutions are {0} and {1}".format (sol1, sol2))

0

1

Updated 2021-09-01

Tags

Python Programming Language

Data Science