Solving Quadratic Equation Using Python

Quadratic equations are defined as ax2 + bx + c = 0 where a, b, c are Real Numbers(or Complex Numbers) and x is a variable.

In High School for solving quadratic equations a formula is taught to kids,which is (-b±√(b²-4ac))/(2a) where different values(a, b, c) are taken from Quadratic equation. Mostly value of b²-4ac is referred to as Determinant of Quadratic Equation. So it can be said that formula for finding roots of a Quadratic Equation is (-b±√(d))/(2a). Do not that for roots to exist determinant value(d) should either be zero or 1.

Quite simple, let’s program this Mathematical Logic as Python Code for Solving Quadratic Equations.

# Finding roots of Quadratic Equation using Python

# Import Python's Math Module
import math

a = float(input("Enter coefficient of x square =>"))
b = float(input("Enter coefficient of x =>"))
c = float(input("Enter constant value in equation =>"))

# Calculating Determinant of Quadratic Equation
d = b**2 - 4*a*c

# Checking if Determinant is positive/zero
if (d >= 0):
	# Calculating Roots of Quadratic Equation
	first_root = (-b + math.sqrt(d))/2*a
	second_root = (-b - math.sqrt(d))/2*a
	print("Roots of Quadratic Equation are =>", first_root,",", second_root)
	print("Roots don't exist")

Output of Above Code

Enter coefficient of x square =>1
Enter coefficient of x =>-4
Enter constant value in equation =>3
Roots of Quadratic Equation are => 3.0 , 1.0


Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at

Leave a Reply

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

Recent Posts