-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadroots.py
More file actions
37 lines (33 loc) · 982 Bytes
/
Quadroots.py
File metadata and controls
37 lines (33 loc) · 982 Bytes
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
#Quadroots.py "PGR" Reference Craig Coleman, who referenced Amit Sarah
def roots(a,b,c):
D = (b*b - 4*a*c)
print()
print("D = " + str(D))
if (D >= 0): #check for positive D
print("REAL ROOTS")
D = D**0.5 # Calculate square root of D
x1 = (-b + D) / (2*a)
x2 = (-b - D)/ (2*a)
print("x1 = " + str(x1)+" x2 = "+str(x2))
elif(D < 0): #check for negative D
D= (D * -1)**0.5; #Change D to a positive
# Take square root of D then represent with "i"
print("IMAGINARY ROOTS")
print ("x1 = -"+str(b/(2*a))+" - "+str(D/(2*a))+"i")
print ("x1 = -"+str(b/(2*a))+" + "+str(D/(2*a))+"i")
if __name__ == '__main__':
print("Input a,b and c for the quadratic (ax^2 + bx + c)")
a = input("Enter a: ")
b = input("Enter b: ")
c = input("Enter c: ")
roots(float(a), float(b), float(c))
'''
Input a,b and c for the quadratic (ax^2 + bx + c)
Enter a: 5
Enter b: 4
Enter c: 2
D = -24.0
IMAGINARY ROOTS
x1 = -0.4 - 0.4898979485566356i
x1 = -0.4 + 0.48989794855663561
'''