Skip to content

Commit b35f0fe

Browse files
Create computeroots.py
1 parent 87bbc9a commit b35f0fe

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

computeroots.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from math import sqrt
2+
3+
print("Quadratic function : (a * x^2) + b*x + c")
4+
a = float(input("a: "))
5+
b = float(input("b: "))
6+
c = float(input("c: "))
7+
8+
r = b**2 - 4*a*c
9+
10+
if r > 0:
11+
num_roots = 2
12+
x1 = (((-b) + sqrt(r))/(2*a))
13+
x2 = (((-b) - sqrt(r))/(2*a))
14+
print("There are 2 roots: %f and %f" % (x1, x2))
15+
elif r == 0:
16+
num_roots = 1
17+
x = (-b) / 2*a
18+
print("There is one root: ", x)
19+
else:
20+
num_roots = 0
21+
print("No roots, discriminant < 0.")
22+
exit()
23+
- See more at: http://www.w3resource.com/python-exercises/math/python-math-exercise-30.php#sthash.gypCq4M0.dpuf

0 commit comments

Comments
 (0)