Skip to content

Commit 86e3394

Browse files
Merge pull request #283 from Prisha-Mordia/main
Add Quadratic Equation Solver Code
2 parents 9c09f64 + 03e3a21 commit 86e3394

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Math/quadratic_equation_solver.py

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

0 commit comments

Comments
 (0)