Skip to content

Commit 8c61a7d

Browse files
Add files via upload
1 parent 9c09f64 commit 8c61a7d

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Math/quadratic_equation_solver.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import cmath
2+
3+
def solve_quadratic(a, b, c):
4+
if a == 0:
5+
raise ValueError("Coefficient 'a' must be non-zero for a quadratic equation.")
6+
7+
discriminant = b**2 - 4 * a * c
8+
9+
root1 = (-b + cmath.sqrt(discriminant)) / (2 * a)
10+
root2 = (-b - cmath.sqrt(discriminant)) / (2 * a)
11+
12+
return root1, root2
13+
14+
try:
15+
a = float(input("Enter coefficient a: "))
16+
b = float(input("Enter coefficient b: "))
17+
c = float(input("Enter coefficient c: "))
18+
19+
roots = solve_quadratic(a, b, c)
20+
print(f"The roots of the equation are: {roots[0]} and {roots[1]}")
21+
except ValueError as e:
22+
print(e)

0 commit comments

Comments
 (0)