Python
Python

 

Introduction

A quadratic equation is a second-order polynomial equation in a single variable x, with the general form:
ax² + bx + c = 0
Where:

  • a, b, and c are constants.
  • a ≠ 0 (if a = 0, the equation becomes linear, not quadratic).

The solution of a quadratic equation can be found using the quadratic formula:
x = (-b ± √(b² - 4ac)) / 2a
This formula gives the two roots (solutions) of the equation, which can be real or complex depending on the discriminant (b² – 4ac).

Objective

The objective of this program is to write a Python script that can solve any quadratic equation of the form ax² + bx + c = 0. The program will:

  • Take the coefficients a, b, and c as input from the user.
  • Calculate the discriminant (b² – 4ac).
  • Determine the nature of the roots based on the discriminant.
  • Calculate and display the real or complex roots accordingly.

Python Code to Solve a Quadratic Equation


def solve_quadratic(a, b, c):
    # Calculate the discriminant
    discriminant = b**2 - 4*a*c

    # Check if discriminant is positive, negative, or zero
    if discriminant > 0:
        # Two real and distinct roots
        root1 = (-b + discriminant**0.5) / (2 * a)
        root2 = (-b - discriminant**0.5) / (2 * a)
        return f"Two real roots: {root1} and {root2}"
    
    elif discriminant == 0:
        # One real root (repeated)
        root = -b / (2 * a)
        return f"One real root: {root}"
    
    else:
        # Complex roots
        real_part = -b / (2 * a)
        imaginary_part = (abs(discriminant)**0.5) / (2 * a)
        return f"Two complex roots: {real_part} + {imaginary_part}i and {real_part} - {imaginary_part}i"

# Main program to input coefficients and solve the equation
def main():
    print("Quadratic Equation Solver")
    a = float(input("Enter the coefficient a: "))
    b = float(input("Enter the coefficient b: "))
    c = float(input("Enter the coefficient c: "))
    
    # Ensure a is not zero
    if a == 0:
        print("Coefficient 'a' cannot be zero for a quadratic equation.")
    else:
        result = solve_quadratic(a, b, c)
        print(result)

# Run the program
if __name__ == "__main__":
    main()
            

Explanation of the Program

The program is designed to solve quadratic equations of the form ax² + bx + c = 0. Here is a breakdown of the program structure:

  • Function Definition: solve_quadratic(a, b, c)This function takes three arguments: the coefficients a, b, and c of the quadratic equation. It calculates the discriminant b² - 4ac and determines whether the roots are real or complex based on the discriminant’s value.
  • Discriminant CalculationIf the discriminant is positive, the function calculates and returns two real roots. If it’s zero, it returns one real repeated root. If it’s negative, it returns two complex roots with real and imaginary parts.
  • Input and Validation: main()The main function prompts the user to input the values of a, b, and c. It checks that a is non-zero (as a quadratic equation requires a ≠ 0). Then, it calls the solve_quadratic() function to calculate and display the result.
  • Running the ProgramTo run the program, simply execute it in a Python environment. It will ask for the input values of a, b, and c. After entering the coefficients, the program will output the roots of the quadratic equation.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)