-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX10.23.py
38 lines (30 loc) · 1.14 KB
/
EX10.23.py
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
# 10.23 (Algebra: solve quadratic equations) Write a function for solving a quadratic
# equation using the following header:
# def solveQuadratic(eqn, roots):
# The coefficients of a quadratic equation ax^2+ bx+c =0 are passed to the list
# eqn and the noncomplex roots are stored in roots. The function returns the
# number of roots. See Programming Exercise 4.1 on how to solve a quadratic
# equation.
# Write a program that prompts the user to enter values for a, b, and c and displays
# the number of roots and all noncomplex roots.
import math
def solveQuadratic(eqn, roots):
a = eqn[0]
b = eqn[1]
c = eqn[2]
discriminant = math.pow(b, 2) - 4 * a * c
if discriminant > 0:
r1 = (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
r2 = (-b - math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
roots.append(r1)
roots.append(r2)
elif discriminant == 0:
r1 = (-b + math.sqrt(b ** 2 - 4 * a * c)) / (2 * a)
roots.append(r1)
return len(roots)
a, b, c = eval(input("Enter a, b, c: "))
eqn = [a, b, c]
roots = []
n = solveQuadratic(eqn, roots)
print("The number of roots:", n)
print("The roots:", roots)