# Python Program to Find Roots of Quadratic Equation

## Grammarly

- More than 40 Lakhs Students use Grammarly for their spell and grammar checks. You can register and try for free.

In this tutorial, you will learn how to Find Roots of Quadratic Equation using input function and the cmath function of the python programming language.

The polynomial equation whose highest degree is two is called a quadratic equation. Quadratic equation is made from a Latin term “quadrates” which means square. The equation is given by **ax² + bx + c = 0**, where a ≠ 0.

Here, “x” is unknown which we have to find and “a”, “b”, “c” specifies numbers or integers such that “a” is not equal to 0. If a = 0 then the equation is not quadratic and becomes liner. The variables a, b and c are called coefficients.

## How to Check if Find Roots of Quadratic Equation?

Let’s take a look at the source code , here the values are given as input by the user in the code, input function and the cmath function carry out the function.

# import complex math module import cmath a = float(input('Enter the value of a: ')) b = float(input('\nEnter the value of b: ')) c = float(input('\nEnter the value of c: ')) # calculate the discriminant d = (b**2) - (4*a*c) # find two solutions sol1 = (-b-cmath.sqrt(d))/(2*a) sol2 = (-b+cmath.sqrt(d))/(2*a) print('\nThe solution are {0} and {1}'.format(sol1,sol2))

**INPUT:**

10 6 8

**OUTPUT:**

Enter a: Enter b: Enter c: The solution are (-0.3-0.8426149773176359j) and (-0.3+0.8426149773176359j)

- At the start, we use the
`import`

function with`cmath`

which allows to access certain system-specific parameters and functions. - We give the user the option to enter the values and the input values are scanned using the
`input`

function and are stored in two variables namely`a`

,`b`

and`c`

with the statements/strings`Enter the value of a:`

,`Enter the value of b:`

and`Enter the value of c:`

with`\n`

respectively. - In the
**STDIN**section of the code editor the input values are entered. - Now we declare the formula to find the discriminant of the quadratic equation, that is
`d = (b**2) - (4*a*c)`

and the value returned is stored in the variable`d`

. Now we move forward to find the two solutions of the quadratic equation. - Declare the formula to find the roots of Quadratic Equation which is
`sol1 = (-b-cmath.sqrt(d))/(2*a)`

, where the function`cmath`

to perform complex square root. The returned value is stored in the variable`sol1`

. - Similarly, we declare the formula to find the roots of Quadratic Equation which is
`sol1 = (-b+cmath.sqrt(d))/(2*a)`

, where the function`cmath`

to perform complex square root. The returned value is stored in the variable`sol2`

. - Now, we display the output value using the
`print`

and display the statement/string`print('\nThe solution are {0} and {1}'.format(sol1,sol2))`

with the`\n`

respectively. - In the above statement/string, the variables
`{0}`

and`{2}`

will hold the values of the variables`sol1`

and`sol2`

where the`format`

function helps in variable substitution and data formatting.

**NOTE:**

- The
**cmath**module provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments. - Using the
**cmath.sqrt()**method, we have calculated two solutions and printed the result. - The
**input()**function allows a user to insert a value into a program, it returns a string value. - The variables used by the format function for substitution is enclosed in curly braces.
- The
**print**statement is followed by a period, to initiate the format function. - The statement for the input function are enclosed in single quotes and parenthesis.
- The
**\n**in the code indicates a new line or the end of a statement line or a string.