Write a program to find the roots of quadratic equation.
Answers
Answered by
1
Answer:
Since you didn't mention language, here it is in python
Explanation:
from math import sqrt
# define ax^2 + bx + c = 0
def roots( a, b, c) :
D = b*b - 4*a*c
print( "Roots of {}x^2 + {}x + {} : ".format(a,b,c))
if D == 0 :
print( -b / (2*a) )
elif D > 0 :
print( (-b + sqrt(D)) / (2*a) )
print( (-b - sqrt(D)) / (2*a) )
else :
print(-b/(2*a), "+", sqrt(-D)/(2*a), "i")
print(-b/(2*a), "-", sqrt(-D)/(2*a), "i")
print()
# x^2 - 4 = 0
roots( 1, 0, -4)
# (x-3)^2 = 0
roots( 1, 6, 9)
# x^2 = -4
roots( 1, 0, 4)
Similar questions