The roots of the quadratic equation ax
2
+ bx + c = 0, where a ≠ 0 are given by the following formula:
In this formula, the term b
2
- 4ac is called the discriminant.
If b
2
- 4ac = 0, then the equation has two equal roots.
If b
2
- 4ac > 0, the equation has two real roots.
If b2 - 4ac < 0, the equation has two complex roots.
Write a program that prompts the user to input the value of a (the coefficient of x
2
), b (the coefficient of x), and c (the constant term)
and outputs the roots of the quadratic equation.
Source Code
import math
a = int(input('Enter the value of a: '))
b = int(input('Enter the value of b: '))
c = int(input('Enter the value of c: '))
d = b*b - 4*a*c
if d == 0:
root1 = (-b) / (2 * a)
root2 = root1
print('The roots are real and equal: Root1 =', root1, ', Root2 =', root2)
elif d > 0:
root1 = (-b + math.sqrt(d)) / (2 * a)
root2 = (-b - math.sqrt(d)) / (2 * a)
print('The roots are real and distinct: Root1 =', root1, ', Root2 =', root2)
else:
print('The roots are imaginary.')
Output
Enter values of a: 2
Enter values of b: 5
Enter values of c: -3
Roots are real & distinct, Root1 = -3.0 Root2 = 0.5