Skip to content
Home » Programming » Python Program to Solve the Quadratic Equation

Python Program to Solve the Quadratic Equation

Python Program to Solve the Quadratic Equation

In this post, we will be investigating the Python Program to Solve the Quadratic Equation. We will solve the quadratic equation and clarify the linguistic structure; simply referred to as syntax of the program.

Algorithm to Solve the Quadratic Equation

Here is the standard form of a quadratic equation;

ax2 + bx + c = 0

where, a, b, and c are real numbers and also a ≠ 0.

However, there are two solutions or roots of this quadratic equation; x1 and x2 given as

x1 = (-b + √(b ** 2 - 4 * a * c) ** 0.5) / 2 * a
x2 = (-b - √(b ** 2 - 4 * a * c) ** 0.5) / 2 * a

And the generalized algorithm to solve the quadratic equation;

Step 1: Start
Step 2: Initialize a, b, and c
Step 3: Calculate the discriminant i.e.
                   dis = (b ** 2) - (4 * a * c)
Step 4: Find the two solutions i.e.
                  x1 = (-b + √(dis) ** 0.5) / 2 * a
                  x2 = (-b - √(dis) ** 0.5) / 2 * a
Step 5: Display the results
Step 6: End

New to Python Programming? Get started with Python Programming!

Solve the Quadratic Equation in Python

Focusing on the Python Program to Solve the Quadratic Equation, let’s jump into the source code.

Example 1: Program with static input

import cmath

a = 2
b = 3
c = 4

# Calculate the discriminant
dis = (b**2) - (4*a*c)

# Find the solutions or roots
x1 = (-b + cmath.sqrt(dis))/(2*a)
x2 = (-b - cmath.sqrt(dis))/(2*a)

print('The solutions are {0} and {1}'.format(x1,x2))

Note: In order to perform the complex mathematical expressions like square root. We need to import complex math (cmath) module.

The output of the above code snippet is;

The solutions are (-0.75+1.1989j) and (-0.75-1.1989j)

Example 2: Program with User Input

import cmath

a = float( input("Enter the value of a: "))
b = float( input("Enter the value of b: "))
c = float( input("Enter the value of c: "))

# Calculate the discriminant
dis = (b**2) - (4*a*c)

# Find the solutions or roots
x1 = (-b + cmath.sqrt(dis))/(2*a)
x2 = (-b - cmath.sqrt(dis))/(2*a)

print('The solutions are {0} and {1}'.format(x1,x2))

The output for above code snippet is;

Enter the value of a: 2
Enter the value of b: 3 
Enter the value of c: 4
The solutions are (-0.75+1.1989j) and (-0.75-1.1989j)

Explanation

According to Wikipedia, a quadratic condition likewise referred to as the Latin quadratus for square is any condition which will be adjusted in standard form as ax2 + bx + c = 0.

As mentioned above a ≠ 0; if a = 0, then the equation is linear that means it will not be a quadratic equation; as there is no ax2 term. The numbers a, b, and c are the coefficients also referred to as quadratic coefficients or constants.

The upsides of x that fulfill the condition are called arrangement or roots or zeros of the articulation on its left-hand side. A quadratic equation may have at most of two roots. If there is only one solution, that means it is a double root.

You may also check —>

Leave a Reply

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