Search This Blog

Wednesday, December 29, 2010

Easy Quadratic equation solution -using cmath library

#Solution of Quadratic equation
from cmath import *
a,b,c=input("Give coefficients in the order a,b,c seperated by comma.  ")
d=sqrt(b*b-4*a*c)
print "Root 1 = ",(-b+d)/(2*a),"Root 2 = ",(-b-d)/(2*a)
 Contributed by Prof. Purushothaman Vadakkedam

import cmath 
print "Hello! This program finds the complex solutions to a quadratic"
print
a, b, c = input("Please enter the coefficients (a, b, c): ")
discRoot = cmath.sqrt(b * b - 4 * a * c) # formula solving discriminant
root1 = (-b + discRoot) / (2 * a) # formula solving root1
root2 = (-b - discRoot) / (2 * a) # formula solving root2
print
print "The solutions are:", root1, root2 # display the results
Contributed by Prof. Ahamed Ashraf

No comments:

Post a Comment