Write a Python program that solves a quadratic equation of the form ax² + bx + c = 0
where a
, b
, and c
are provided by the user.
Input-Output Examples
plaintext
Example 1:
Input:
Enter a: 1
Enter b: -3
Enter c: 2
Output: The roots of the quadratic equation are: 2.0 and 1.0
Example 2:
Input:
Enter a: 1
Enter b: 2
Enter c: 1
Output: The root of the quadratic equation is: -1.0
Algorithm to solve the quadratic equation
- Start
- Take input: Prompt the user to enter the coefficients
a
,b
, andc
of the quadratic equation. - Calculate the discriminant: Compute the discriminant using the formula
D = b² - 4ac
. - Check the nature of the roots:
- If
D > 0
, the equation has two real and distinct roots. - If
D == 0
, the equation has two equal roots (one real root). - If
D < 0
, the equation has complex roots.
- If
- Compute the roots using the quadratic formula:
Root 1 = (-b + √D) / 2a
Root 2 = (-b - √D) / 2a
- Display the result: Print the calculated roots.
- End
Python Program to solve the Quadratic Equation
python
# Python program to solve a quadratic equation
# Importing the math module to use the sqrt() function
import math
# Take input from the user for coefficients a, b, and c
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
# Calculate the discriminant
discriminant = b**2 - 4*a*c
# Check the nature of the discriminant and find the roots accordingly
if discriminant > 0:
# Two real and distinct roots
root1 = (-b + math.sqrt(discriminant)) / (2*a)
root2 = (-b - math.sqrt(discriminant)) / (2*a)
print(f"The roots of the quadratic equation are: {root1} and {root2}")
elif discriminant == 0:
# One real root (both roots are equal)
root = -b / (2*a)
print(f"The root of the quadratic equation is: {root}")
else:
# Complex roots
real_part = -b / (2*a)
imaginary_part = math.sqrt(abs(discriminant)) / (2*a)
print(f"The roots of the quadratic equation are: {real_part}+{imaginary_part}i and {real_part}-{imaginary_part}i")
Code Explanation
- Importing the “math” module:
Themath
module is imported to use thesqrt()
function for calculating the square root of the discriminant. - Taking input from the user:
The user provides the values for the coefficientsa
,b
, andc
of the quadratic equation. These are converted tofloat
to handle both integers and decimals. - Calculating the discriminant:
The discriminant is computed using the formulaD = b² - 4ac
. This helps determine the nature of the roots:- If
D > 0
, the roots are real and distinct. - If
D == 0
, the roots are real and equal. - If
D < 0
, the roots are complex (having real and imaginary parts).
- If
- Finding the roots:
The quadratic formula(-b ± √D) / 2a
is used to compute the roots based on the discriminant:- For real roots, the square root of the discriminant is calculated directly.
- For complex roots, the real and imaginary parts are calculated separately.
- Displaying the output:
Theprint()
function is used to display the roots of the equation, formatted based on whether the roots are real or complex.