Python Program Quadratic Equation Roots

By | July 27, 2020

Task: Python Program Quadratic Equation Roots, Solution of Quadratic equation, Find roots of quadratic equation.

How This Program Works

This program uses cmath module and sqrt() builtin function to find the roots of the given quadratic equation when the coefficients a,b and c are given.

The popular quadratic equation formula for solving it

is as under:

Python Program Quadratic Equation Roots

Source Code Python Program Quadratic Equation Roots

# This Python program finds roots of
# a given quadratic equation with given
# coefficients a,b and c.
# Solve the quadratic equation ax**2 + bx + c = 0

# import complex math module
from cmath import *

a = float(input("Enter the value of Coefficient a: "))
b = float(input("Enter the value of Coefficient b: "))
c = float(input("Enter the value of Coefficient c: "))

# use the following formula to calculate the discriminant
d = (b**2) - (4*a*c)

# find two solutions /  roots of quadratic equation
solution1 = (-b-sqrt(d))/(2*a)
solution2 = (-b+sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(solution1,solution2))

Output:

Enter the value of Coefficient a: 1
Enter the value of Coefficient b: -3
Enter the value of Coefficient c: 2
The solution are (1+0j) and (2+0j)

Output 2:

Enter the value of Coefficient a: 1
Enter the value of Coefficient b: 5
Enter the value of Coefficient c: 6
The solution are (-3+0j) and (-2+0j)


Loading

Leave a Reply

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