Python Program to solve the Quadratic Equation

tutorials

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

  1. Start
  2. Take input: Prompt the user to enter the coefficients a, b, and c of the quadratic equation.
  3. Calculate the discriminant: Compute the discriminant using the formula D = b² - 4ac.
  4. 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.
  5. Compute the roots using the quadratic formula:
    • Root 1 = (-b + √D) / 2a
    • Root 2 = (-b - √D) / 2a
  6. Display the result: Print the calculated roots.
  7. 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

  1. Importing the “math” module:
    The math module is imported to use the sqrt() function for calculating the square root of the discriminant.
  2. Taking input from the user:
    The user provides the values for the coefficients a, b, and c of the quadratic equation. These are converted to float to handle both integers and decimals.
  3. Calculating the discriminant:
    The discriminant is computed using the formula D = 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).
  4. 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.
  5. Displaying the output:
    The print() function is used to display the roots of the equation, formatted based on whether the roots are real or complex.
tools

Python

Related Articles