Python program to solve quadratic equation

Quadratic equations are a fundamental part of higher-level mathematics, and many real-world problems can be expressed as quadratic equations. The solution to a quadratic equation can provide valuable insights into complex situations that may seem insurmountable at first glance.

In this article, we will discuss the basics of quadratic equations and their solutions. We will also present a Python program to solve a quadratic equation.

What are Quadratic Equations?

Quadratic equations are algebraic expressions in which the highest exponent is 2. These equations take the form:

ax^2 + bx + c = 0

where a, b, and c are constants. The value of x that satisfies the equation is known as the root(s) of the equation.

Quadratic equations can be solved by various methods, but the two primary methods are completing the square and using the quadratic formula.

How to Solve a Quadratic Equation?

Using the Quadratic Formula:

The quadratic formula is a well-known formula in mathematics, which is used to find the roots of a quadratic equation. The formula is represented as:

x = (-b ± √(b^2 - 4ac)) / 2a

In this formula, a, b, and c are constants, and the plus/minus symbol indicates that there can be two possible values of x.

Completing the Square:

Completing the square is a method used to solve quadratic equations by turning them into a perfect square. The steps involved in completing the square are:

  • Move the constant to the right side of the equation.
  • Divide the entire equation by the coefficient of x^2
  • Add and subtract the square of half the coefficient of x to the left-hand side of the equation.
  • Simplify.
  • Square root both sides of the equation.
  • Solve for x.

Python Program to Solve a Quadratic Equation:

Here is an example Python program that will solve a quadratic equation using the quadratic formula:

import cmath
a = 1
b = 5
c = 6

d = (b**2) - (4*a*c)

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

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

Expected output:

The solutions are (-3+0j) and (-2+0j)


Q: What will happen if the value under the square root term in the quadratic formula is negative?

A: If the value under the square root term in the quadratic formula is negative, the equation has no real roots. However, it has two complex conjugate roots.

Q: What if a is equal to zero in the quadratic equation?

A: If a is equal to zero in the quadratic equation, you will end up with a linear equation, which can be solved by basic algebraic methods.

Q: Can a quadratic equation have more than two real roots?

A: No, a quadratic equation can have at most two real roots.

Q: How do I know if my answer to a quadratic equation is correct?

A: You can check your answer by plugging it back into the original quadratic equation and ensuring that the equation satisfies the roots.

Q: Are there any limitations when using the quadratic formula to solve equations?

A: Yes, the quadratic formula cannot be applied to equations with complex roots, and it may be computationally intensive when dealing with large numbers. In such cases, other methods such as completing the square may be more efficient.


In conclusion, quadratic equations are an essential part of higher-level mathematics, and their solutions can provide valuable insights into complex situations. By understanding the quadratic formula and methods such as completing the square, you can solve quadratic equations efficiently. We hope the provided Python program and FAQs related will help you get started with solving quadratic equations.


Table of Contents

Related posts