Created
May 2, 2019 11:26
-
-
Save skanel/5360dd687d9eb350d64d0658fe422ecb to your computer and use it in GitHub Desktop.
Python quadratic equation solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import math | |
#quadraticpy | |
# quadratic formula: (-b + or - sqrt(b^2 - 4ac)) / 2a | |
# define constant | |
# output is limited to 2 decimal places, can be changed below | |
dp = "%.2f" | |
def quadratic(): | |
# get the coefficients from the user | |
a = float(input("Please enter coefficient a: ")) | |
b = float(input("Please enter coefficient b: ")) | |
c = float(input("Please enter coefficient c: ")) | |
discRoot = math.sqrt((b * b) - 4 * a * c) # first pass | |
root1 = (-b + discRoot) / (2 * a) # solving positive | |
root2 = (-b - discRoot) / (2 * a) # solving negative | |
print() | |
print("The solutions are:", dp % root1, dp % root2) | |
if __name__ == '__main__': | |
quadratic() | |
else: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment