From 8c61a7d65195084e47cbbe17efff624cdff4baf3 Mon Sep 17 00:00:00 2001 From: Prisha Mordia <130738859+Prisha-Mordia@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:50:03 +0530 Subject: [PATCH 1/2] Add files via upload --- Math/quadratic_equation_solver.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Math/quadratic_equation_solver.py diff --git a/Math/quadratic_equation_solver.py b/Math/quadratic_equation_solver.py new file mode 100644 index 0000000..e3fbcda --- /dev/null +++ b/Math/quadratic_equation_solver.py @@ -0,0 +1,22 @@ +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) From 03e3a21e41500016ff6b9d9fd2ade9a8b954199d Mon Sep 17 00:00:00 2001 From: Punit Choudhary <65050160+Punit-Choudhary@users.noreply.github.com> Date: Thu, 31 Oct 2024 23:52:29 +0530 Subject: [PATCH 2/2] Fix linting issues --- Math/quadratic_equation_solver.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Math/quadratic_equation_solver.py b/Math/quadratic_equation_solver.py index e3fbcda..f38e222 100644 --- a/Math/quadratic_equation_solver.py +++ b/Math/quadratic_equation_solver.py @@ -1,5 +1,6 @@ import cmath + def solve_quadratic(a, b, c): if a == 0: raise ValueError("Coefficient 'a' must be non-zero for a quadratic equation.") @@ -8,14 +9,15 @@ def solve_quadratic(a, b, 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: