Q:

Write a Python program to solve quadratic equations aX^2+bX+C=0

0

Write a Python program to solve quadratic equations aX^2+bX+C=0

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#import math module
from math import sqrt

#read a,b,c values
a=float(input('Enter a:'))
b=float(input('Enter b:'))
c=float(input('Enter c:'))

#calculate the discriminant
d=(b**2)-(4*a*c)

#find solutions
if d>0:  #number of roots is 2
    root1=(((-b)+sqrt(d))/(2*a))
    root2=(((-b)-sqrt(d))/(2*a))
    print("There are 2 roots: %f and %f" %(root1,root2))
elif d==0:  #number of roots is 1
    root=(-b)/2*a
    print("There is one root: ",root)
else:  #no roots for the formula
    print("No root, discriminant <0")

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now