forked from powerexploit/Awesome-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquad_equation.py
More file actions
43 lines (37 loc) · 1.62 KB
/
Copy pathquad_equation.py
File metadata and controls
43 lines (37 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Python Program to find roots of a Quadratic Equation
# Solve the quadratic equation ax**2 + bx + c = 0
import math # import complex math module
def findRoots(a, b, c):
"""
If a is 0 then equation is not quadratic but linear,
So if user enters value of a as zero then its invalid.
"""
if a == 0:
print("Invalid")
return -1
discriminant = (b * b) - (4 * a * c) # calculating value of discriminant
"""
Now below we check for various condition, i.e
Is discriminant greater than, equals to or less than 0.
Each condition has its own different roots.
"""
if(discriminant > 0):
root_1 = (-b + math.sqrt(discriminant) / (2 * a))
root_2 = (-b - math.sqrt(discriminant) / (2 * a))
print("Two Distinct Real Roots Exists: ")
print("root1 = %.2f and root2 = %.2f" % (root_1, root_2))
elif(discriminant == 0):
root_1 = root_2 = -b / (2 * a)
print("Two Equal and Real Roots Exists: ")
print("root1 = %.2f and root2 = %.2f" % (root_1, root_2))
elif(discriminant < 0):
root_1 = root_2 = -b / (2 * a)
imaginary = math.sqrt(-discriminant) / (2 * a)
print("Two Distinct Complex Roots Exists: ")
print("r1=%.2f+%.2f and r2=%.2f-%.2f" % (root_1, imaginary, root_2, imaginary))
# Main Program
if __name__ == "__main__":
a = float(input("Please Enter a Value of a Quadratic Equation : "))
b = float(input("Please Enter b Value of a Quadratic Equation : "))
c = float(input("Please Enter c Value of a Quadratic Equation : "))
print(findRoots(a, b, c))