Articles

# Program to find the roots of a quadratic equation in Python

In this python programming guide, we are going to learn,

• program to find the roots of a quadratic equation in python

### Python program to find the roots of a Quadratic Equation

The program to find the roots of a quadratic equation in Python is as follows:

``````# Owner : TutorialsInhand Author : Devjeet Roy

import math

a = int(input("Enter a: "))
b = int(input("Enter b: "))
c = int(input("Enter c: "))

if a == 0:
print("Enter the coefficients correctly.")
else:
disc = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(disc))

if disc > 0:
print("Real and different roots ")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))

elif disc == 0:
print("Real and same roots")
print(-b / (2 * a))

else:
print("Complex Roots exist.")
print(- b / (2 * a), " + i", sqrt_val)
print(- b / (2 * a), " - i", sqrt_val)``````

The output of python program to find the roots of a quadratic equation is as follows:

``````PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
Enter a: 10
Enter b: 22
Enter c: 3
Real and different roots
-0.14606079858305437
-2.053939201416946``````

1. If b2 < 4ac then roots are complex.

2. If b2 == 4ac then roots are real and equal.

3. If b2 > 4ac then roots are real and different.

Given below is snapshot for python program to find the roots of a quadratic equation:

Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com