1. Python

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)
else:
	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

Comments to: Solving Quadratic Equation Using Python

Your email address will not be published.

Ads