Solving a Quadratic Equation

A code to find the roots of a quadratic equation.

Welcome to the code! Wanna solve your math problem on the quadratic equation? Here’s a code to solve your quadratic equation, let your compiler solve your problem while you can just rest and watch it happen.

In this code, you need to understand that the standard form of a quadratic equation is ax2+bx+c = 0; where a, b, c are real numbers and a ≠ 0.

The user has to give the values of a, b & c, and the rest of the code will be responsible to find the solutions to the given equation.

Note: Loved the post? You too can publish your article on “Python for fun” which will be loved by millions. Contribute an article!

					# Solving the quadratic equation ax**2 + bx + c = 0

# import complex math module
import cmath

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

# calculating the discriminant
d = (b**2) - (4*a*c)

# finding 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))				
Share on facebook
Share on whatsapp
Share on pinterest
0 0 votes
Notify of
Inline Feedbacks
View all comments