Skip to content

Commit

Permalink
Merge pull request #283 from Prisha-Mordia/main
Browse files Browse the repository at this point in the history
Add Quadratic Equation Solver Code
  • Loading branch information
Punit-Choudhary authored Oct 31, 2024
2 parents 9c09f64 + 03e3a21 commit 86e3394
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Math/quadratic_equation_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import cmath


def solve_quadratic(a, b, c):
if a == 0:
raise ValueError("Coefficient 'a' must be non-zero for a quadratic equation.")

discriminant = b**2 - 4 * a * c

root1 = (-b + cmath.sqrt(discriminant)) / (2 * a)
root2 = (-b - cmath.sqrt(discriminant)) / (2 * a)

return root1, root2


try:
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))

roots = solve_quadratic(a, b, c)
print(f"The roots of the equation are: {roots[0]} and {roots[1]}")
except ValueError as e:
print(e)

0 comments on commit 86e3394

Please sign in to comment.