Skip to content

Instantly share code, notes, and snippets.

@skanel
Created May 2, 2019 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skanel/5360dd687d9eb350d64d0658fe422ecb to your computer and use it in GitHub Desktop.
Save skanel/5360dd687d9eb350d64d0658fe422ecb to your computer and use it in GitHub Desktop.
Python quadratic equation solver
#!/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