Here is a Python program that solves a quadratic equation of the form ax^2 + bx + c = 0:

Created with Sketch.

Here is a Python program that solves a quadratic equation of the form ax^2 + bx + c = 0:

import math

a = float(input("Enter the coefficient of x^2: "))
b = float(input("Enter the coefficient of x: "))
c = float(input("Enter the constant: "))

# Calculate the discriminant
d = b ** 2 - 4 * a * c

# Find the two solutions of the quadratic equation
if d < 0:
    print("This equation has no real solutions.")
elif d == 0:
    x = (-b + math.sqrt(d)) / (2 * a)
    print("The solution is: ", x)
else:
    x1 = (-b + math.sqrt(d)) / (2 * a)
    x2 = (-b - math.sqrt(d)) / (2 * a)
    print("The solutions are: ", x1, " and", x2)

The program prompts the user to enter the coefficients of the quadratic equation using the input() function, and stores them in variables a, b, and c.

It then calculates the discriminant (d) using the formula b^2 – 4ac.

The program then uses an if-elif-else statement to check the value of the discriminant and find the two solutions of the equation using the quadratic formula. If the discriminant is less than 0, the equation has no real solutions. If the discriminant is equal to 0, the equation has only one solution. Else, the equation has two solutions.

Please note that the math library should be imported to use sqrt() function.

Leave a Reply

Your email address will not be published. Required fields are marked *