Python Program For Quadratic Equation (With Code)

Python Program For Quadratic Equation

In this guide, you will learn about the Python program for quadratic equation.

In this article, we will explore how to write a Python program to solve quadratic equations.

Quadratic equations are polynomial equations of degree 2, and they play a significant role in various mathematical and scientific applications.

By using the quadratic formula, we can find the roots of a quadratic equation, which represent the points where the equation intersects the x-axis.

Section 1

Python Program for Quadratic Equation

To solve a quadratic equation using a Python program, we need to obtain the coefficients of the quadratic equation from the user and apply the quadratic formula.

The quadratic formula states that for a quadratic equation in the form of ax2 + bx + c = 0, the solutions are given by the formula:

x = b ± b 2 4 a c 2 a

Here is the python program for quadratic equation.

Python Program for Quadratic Equation

import math

def solve_quadratic_equation(a, b, c):
    discriminant = b**2 - 4*a*c

    if discriminant > 0:
        root1 = (-b + math.sqrt(discriminant)) / (2*a)
        root2 = (-b - math.sqrt(discriminant)) / (2*a)
        return root1, root2
    elif discriminant == 0:
        root = -b / (2*a)
        return root
    else:
        return "Complex roots"

# Example usage
a = 1
b = -5
c = 6
roots = solve_quadratic_equation(a, b, c)
print("Roots:", roots)

You can run this code on our free Online Python Compiler.

Output

Roots: (3.0, 2.0)

Let’s break down the above program into smaller steps to understand it better.

Breakdown

Understanding Python Program for Quadratic Equation

Obtaining Coefficients from the User

To solve a quadratic equation, we need to obtain the values of coefficients a, b, and c from the user.

These coefficients represent the quadratic, linear, and constant terms of the equation, respectively.

We can use the input() function to prompt the user for these values:

a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))

Calculating the Discriminant: Python Program For Quadratic Equation

The discriminant, denoted by D, is a crucial value in the quadratic formula as it determines the nature of the roots.

We calculate the discriminant using the formula D = b2 - 4ac.

In our program, we calculate the discriminant as follows:

discriminant = b**2 - 4*a*c

Determining the Nature of Roots

Based on the value of the discriminant, we can determine the nature of the roots.

If the discriminant is positive, the equation has two real and distinct roots.

And if the discriminant is zero, the equation has a single real root.

If the discriminant is negative, the equation has complex roots.

Calculating the Roots

To calculate the roots of the quadratic equation, we use the quadratic formula.

The formula involves taking the square root of the discriminant and performing some arithmetic operations. In our program, we calculate the roots as follows.

Calculating Roots: Python Program for Quadratic Equation

if discriminant > 0:
    root1 = (-b + math.sqrt(discriminant)) / (2*a)
    root2 = (-b - math.sqrt(discriminant)) / (2*a)
    return root1, root2
elif discriminant == 0:
    root = -b / (2*a)
    return root
else:
    return "Complex roots"

The function returns the roots as a tuple when they are real and distinct.

If the roots are real and equal, it returns a single value.

If the roots are complex, it returns the string “Complex roots”.

Printing the Roots

In our example, we print the roots using the print() function:

print("Roots:", roots)

Testing

Running Python Program for Quadratic Equation

Here is the complete python program for quadratic equations in action.

Python Program for Quadratic Equation

import math

def solve_quadratic_equation(a, b, c):
    discriminant = b**2 - 4*a*c

    if discriminant > 0:
        root1 = (-b + math.sqrt(discriminant)) / (2*a)
        root2 = (-b - math.sqrt(discriminant)) / (2*a)
        return root1, root2
    elif discriminant == 0:
        root = -b / (2*a)
        return root
    else:
        return "Complex roots"

# Example usage
a = 1
b = -5
c = 6
roots = solve_quadratic_equation(a, b, c)
print("Roots:", roots)

You can run this code on our free Online Python Compiler.

Output

Roots: (3.0, 2.0)

FAQs

FAQs About Python Program for Quadratic Equation

Q: What is a quadratic equation?

A quadratic equation is a second-degree polynomial equation of the form ax2 + bx + c = 0, where a, b, and c are constants and x is the variable.

It represents a parabolic curve and can have two, one, or zero real solutions.

Example: 2x2 + 3x – 5 = 0

A quadratic equation can be solved using various methods, such as factoring, completing the square, or by using the quadratic formula.

Q: What is the quadratic formula?

The quadratic formula is a formula used to solve quadratic equations of the form ax2 + bx + c = 0.

It provides a solution for x in terms of the coefficients a, b, and c.

The quadratic formula is given by:

x = b ± b 2 4 a c 2 a

By substituting the values of a, b, and c into the formula, we can calculate the roots of the quadratic equation.

Q: How many roots can a quadratic equation have?

A quadratic equation can have a maximum of two real roots.

These roots can be real and distinct (two different values), real and equal (the same value repeated), or complex conjugate pairs (involving the imaginary unit i).

Q: Can a quadratic equation have no real roots?

Yes, a quadratic equation can have no real roots if the discriminant (b2 - 4ac) is negative.

In this case, the roots are complex conjugate pairs and involve the imaginary unit i.

The complex roots are not represented on the x-axis.

Q: Can we solve quadratic equations using Python?

Yes, Python provides the necessary mathematical functions and operations to solve quadratic equations.

By using the quadratic formula and implementing it in a Python program, we can find the roots of quadratic equations.

Q: How can I use the quadratic equation program for different equations?

To use the quadratic equation program for different equations, you need to modify the values of the coefficients a, b, and c in the program.

Simply assign the desired values to these variables, and the program will calculate the roots accordingly.

Q: How to do quadratic equation in Python?

To solve a quadratic equation in Python, you can write a program that prompts the user for the coefficients of the equation (a, b, and c) and then applies the quadratic formula to calculate the roots.

The program can utilize the math module to perform the necessary mathematical operations.

Q: How do you write a quadratic equation in code?

To write a quadratic equation in code, you can define the coefficients (a, b, and c) as variables and use them in the quadratic formula.

The equation can be represented as ax^2 + bx + c = 0, where ^ denotes exponentiation.

By applying the quadratic formula, you can find the roots of the equation.

Q: How do you find the root of an equation in Python?

To find the root of an equation in Python, you can use numerical methods or algebraic methods depending on the type of equation.

For quadratic equations, you can use the quadratic formula.

By implementing the formula in a Python program and substituting the appropriate coefficients, you can calculate the roots of the equation.

Q: What will get printed for a=1, b=-5, and c=6?

For the given values of a=1, b=-5, and c=6, the program will calculate the roots of the quadratic equation.

In this case, the roots will be real and distinct.

The program will print the roots as follows:

Roots: (3.0, 2.0)

The roots are (3.0, 2.0), indicating that the equation intersects the x-axis at x=3 and x=2.

Remember to input the coefficients accurately when executing the program to obtain the correct roots for the quadratic equation.

Wrapping Up

Conclusions: Python Program for Quadratic Equation

In this article, we learned how to write a Python program to solve quadratic equations.

We explored the quadratic formula and its implementation in Python code.

By obtaining the coefficients from the user and applying the quadratic formula, we can find the roots of quadratic equations.

Python provides a convenient and efficient way to solve mathematical problems, including quadratic equations.

Remember to utilize this Python program whenever you encounter quadratic equations in your mathematical or scientific endeavors.

It can save you time and effort by automatically calculating the roots for you.

Experiment with different coefficients and equations to deepen your understanding of quadratic equations and Python programming.

Now that you have the knowledge and tools to solve quadratic equations using Python, why not put it into practice and see the results for yourself?

Happy coding!


Discover more from Python Mania

Subscribe to get the latest posts sent to your email.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Related Articles:

Recent Articles:

0
Would love your thoughts, please comment.x
()
x

Discover more from Python Mania

Subscribe now to keep reading and get access to the full archive.

Continue reading