Python Program: To Find Roots of a Quadratic Equation with Explanation

DDSRY
2 min readMay 26, 2020

--

Hey, Do you want the Python code for writing a program to Find Roots of a Quadratic Equation in Python, Python Program Explanation?

Python Program: How to Find roots of a quadratic equation with program explanation

Let’s know the standard form of a quadratic equation : ax2 + bx + c = 0

  • Where in Formula, a, b, and c are coefficients?
  • Remember This: the value of a cannot be 0.

Its roots are:

Python By DDSRY

Python Code:

import cmath # import complex math module

Coefficient of a Quadratic Equation:

a=float(input(“Enter ‘a’ value : “))
b=float(input(“Enter ‘b’ value : “))
c=float(input(“Enter ‘c’ value : “))

d=(b*2)-(4a*c) # It’s Calculate the Discriminant

Two Solutions

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

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

print(‘The roots are : ‘)
print(root1)
print(root2)

  • Below is the Explanation of Python Program.

Image of a Python code:

Python Program: How to find roots of a quadratic equation with coding explanation by DDSRY

Python Program Explanation:

  • Here, we have imported the cmath module, so that we can perform the square root operation using sqrt() function.
  • First, we calculate the discriminant using the formula below,
  • Formula: d=(b**2) — (4*a*c)
  • Then, we have found the two solutions of the quadratic equation using the formula,
  • Formula:

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

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

To Know more Click here.

Leave a Comment Below:

--

--

No responses yet