## Solve Quadratic Equation using Python

- Quadratic Equation
- Discriminant value
- Calculating roots of Quadratic Equation
- Types of roots
- Approach
- Implementation

**Quadratic equation in python: **Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.

### 1)Quadratic Equation

**Python quadratic formula: **Quadratics or quadratic equations are polynomial equations of the second degree, which means that they contain at least one squared word.

ax2 + bx + c = 0

where x is an unknown variable and the numerical coefficients a , b , c.

### 2)Discriminant value

Discriminant = b ^ 2 - 4 * a *c

Based on the value of discriminant there are three types of roots for Quadratic Equation

- How to Check the Nature of Roots of a Quadratic Equation in Python?
- Java Program to Find the Roots of Quadratic Equation
- Python Program to Calculate the Discriminant Value

### 3)Calculating roots of Quadratic Equation

roots = ( -b + sqrt(b ^ 2 - 4 * a *c) ) / (2 * a) , ( -b - sqrt(b ^ 2 - 4 * a *c) ) / (2 * a)

Where sqrt is square root.

### 4)Types of roots

**i)Real and distinct roots**

When the Value of discriminant is greater than 0 then there exist two distinct roots for the quadratic equation

which can be calculated using the above roots formula.

**Examples:**

**Input:**

a = 2 b = -7 c = 6

**Output:**

The two distinct roots are : (2+0j) (1.5+0j)

**ii)Real and equal roots**

When the Value of discriminant is equal to 0 then there exist two equal roots for the quadratic equation .

which can be calculated using the above roots formula.

**Examples:**

**Input:**

a = 1 b = -4 c = 4

**Output:**

The two equal roots are : 2.0 2.0

**iii)Complex roots**

When the Value of discriminant is greater than 0 then there exist two complex roots for the quadratic equation .

which can be calculated using the above roots formula.

**Examples:**

**Input:**

a = 5 b = 2 c = 3

**Output:**

There exists two complex roots: (-1+1.7320508075688772j) (-1-1.7320508075688772j)

### 5)Approach

- To perform complex square root, we imported the
**cmath**module. - First, we compute the discriminant.
- Using
**if..elif..else**we do the below steps - If the value of discriminant is greater than 0 then we print real roots using mathematical formula.
- If the value of discriminant is equal to 0 then we print two equal roots using mathematical formula.
- If the value of discriminant is less than 0 then we print two complex roots using mathematical formula.

### 6)Implementation:

**Below is the implementation:**

# importing cmath import cmath # given a,b,c values a = 2 b = -7 c = 6 discriminant = (b**2) - (4*a*c) # checking if the value of discriminant is greater than 0 if(discriminant > 0): # here exist the two distinct roots and we print them # calculating the roots root1 = (-b+discriminant) / (2 * a) root2 = (-b-discriminant) / (2 * a) # printing the roots print("The two distinct roots are : ") print(root1) print(root2) # checking if the value of discriminant is equal to 0 elif(discriminant == 0): # here exist the two equal roots # calculating single root here discriminant is 0 so we dont need to write full formulae root = (-b)/(2*a) # printing the root print("The two equal roots are : ") print(root, root) # else there exists complex roots else: # here exist the two complex roots # calculating complex roots realpart = -b/(2*a) complexpart = discriminant/(2*a)*(-1) # printing the roots print("There exists two complex roots:") print(realpart, "+", complexpart, "i") print(realpart, "-", complexpart, "i")

**Output:**

The two distinct roots are : (2+0j) (1.5+0j)

**Related Programs**:

- python program to compute a polynomial equation given that the coefficients of the polynomial are stored in a list
- python program to solve maximum subarray problem using kadanes algorithm
- java program to find the roots of a quadratic equation
- python program to find the factorial of a number
- python program to check armstrong number
- python program to convert kilometers to miles and vice versa
- python program to find the largest among three numbers