Advertisement
Lulz-Tigre

QuadracticEQN

Apr 21st, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import math
  3.  
  4. #quadraticpy
  5. # quadratic formula: (-b + or - sqrt(b^2 - 4ac)) / 2a
  6. # define constant
  7. # output is limited to 2 decimal places, can be changed below
  8.  
  9. dp = "%.2f"
  10.  
  11. def quadratic():
  12.  
  13.     # get the coefficients from the user
  14.     a = float(input("Please enter coefficient a: "))
  15.     b = float(input("Please enter coefficient b: "))
  16.     c = float(input("Please enter coefficient c: "))
  17.  
  18.     discRoot = math.sqrt((b * b) - 4 * a * c) # first pass
  19.     root1 = (-b + discRoot) / (2 * a) # solving positive
  20.     root2 = (-b - discRoot) / (2 * a) # solving negative
  21.  
  22.     print()
  23.     print("The solutions are:",  dp % root1, dp % root2)
  24.    
  25. if __name__ == '__main__':
  26.     quadratic()
  27. else:
  28.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement