Python program to find roots of quadratic equation
In this post, you will learn how to find the roots of quadratic equation using the Python programming language.
A quadratic equation in math is a second-degree equation of the given form.
ax² + bx + c = 0
Here a, b, are the coefficients, c is the constant term, and x is the variable. Since the variable x is of the second degree, there are two roots or answers to this quadratic equation.
The universal rule of quadratic equations defines that the value of 'a' cannot be zero, and the value of x is used to find the roots of the quadratic equation (a, b).
Roots of quadratic equation
The discriminant of a quadratic equation is calculated as b2 - 4ac. The nature of the roots depends on the Discriminant (D),
- If D > 0, the roots are real and distinct (unequal)
- If D = 0, the roots are real and equal.
- If D < 0, the roots are real and imaginary.
The given table represents the nature of the quadratic equation roots -
If discriminant(D) > 0 | |
If discriminant = 0 | |
If discriminant < 0 |
Algorithm of Roots of quadratic equation
1. Start
2. Input the coefficient variable, a, b and c.
3. D <- sqrt (b * b - 4 * a * c).
4. R1 <- (-b + D) / ( 2 * a).
5. R2 <- (-b - D) / (2 * a).
6. Print the roots R1 and R2.
7. Stop
Python Program to Find Roots of a Quadratic Equation
import math
a = float(input("Insert coefficient a: "))
b = float(input("Insert coefficient b: "))
c = float(input("Insert coefficient c: "))
discriminant = b**2 - 4 * a * c
if discriminant >= 0:
root1=(-b+math.sqrt(discriminant))/2*a
root2=(-b-math.sqrt(discriminant))/2*a
else:
root1= complex((-b/(2*a)),math.sqrt(-discriminant)/(2*a))
root2= complex((-b/(2*a)),-math.sqrt(-discriminant)/(2*a))
if discriminant > 0:
print("Roots are real and different: {} and {}".format(root1,root2))
elif discriminant == 0:
print("Roots are real and same: ", root1)
else:
print("Roots are complex: {} and {}".format(root1,root2))
Output of the above code:
Insert coefficient a: 4.3
Insert coefficient b: 3.2
Insert coefficient c: 5.2
Roots are complex: (-0.372093023255814+1.034818393548232j) and (-0.372093023255814-1.034818393548232j)
Insert coefficient a: 4.2
Insert coefficient b: 6.3
Insert coefficient c: 2.3
Roots are real and different: -11.078140339148488 and -15.381859660851513
Insert coefficient a: 2
Insert coefficient b: -1.3
Insert coefficient c: 4.2
Roots are complex: (0.325+1.41222342424986j) and (0.325-1.41222342424986j)
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary