Python Program For Solving Quadratic Equation.
This program first takes input values of the coefficients of the quadratic equation (a, b, and c). It then calculates the discriminant (d) using the formula (b^2 - 4ac).
It then checks the value of the discriminant to determine the nature of the roots. If the discriminant is negative, it means that no real roots exist. If it is zero, it means that both roots are equal. And if it is positive, it means that there are two distinct roots.
Finally, the program calculates and prints the roots of the quadratic equation.
Input Program :
import math
# Taking input values of coefficients of the quadratic equation
a = float(input("Enter the coefficient of x^2: "))
b = float(input("Enter the coefficient of x: "))
c = float(input("Enter the constant term: "))
# Calculating the discriminant
d = (b**2) - (4*a*c)
# Checking for the nature of roots
if d < 0:
print("No real roots exist.")
elif d == 0:
# Calculating the root when discriminant is 0
root = (-b) / (2*a)
print(f"Both roots are equal and is {root}.")
else:
# Calculating the roots when discriminant is positive
root1 = (-b + math.sqrt(d)) / (2*a)
root2 = (-b - math.sqrt(d)) / (2*a)
print(f"The roots are {root1} and {root2}.")