# Python Program to Solve Quadratic Equation

In this post, we will learn how to solve a quadratic equation using Python Programming language.

This program asks the user to enter the values of a, b and c, then it computes the roots of the quadratic equation using cmath.

So, without further ado, letâ€™s begin this tutorial.

## Python Program to Solve Quadratic Equation

```# Python Program to Solve Quadratic Equation
import cmath

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

# Calculating discriminant
d = (b**2) - (4*a*c)

# finding roots
root1 = (-b-cmath.sqrt(d)) / (2 * a)
root2 = (-b+cmath.sqrt(d)) / (2 * a)
print("The Roots are {0} and {1}" .format(root1, root2))
```

Output

``````Enter the value of a: 2
Enter the value of b: 1
Enter the value of c: -6
The Roots are (-2+0j) and (1.5+0j)
``````

## How Does This Program Work ?

```import cmath
```

We import the cmath module in our program in order to perform complex square root.

```# Asking for input
a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))
c = int(input("Enter the value of c: "))
```

The user is asked to enter the values of a, b and c.

```# Calculating discriminant
d = (b**2) - (4*a*c)
```

Then, we calculate the discriminant using the formula b2 – 4ac.

```# finding roots
root1 = (-b-cmath.sqrt(d)) / (2 * a)
root2 = (-b+cmath.sqrt(d)) / (2 * a)
```

Now, we calculated the square roots using the formula [(-b + (4ac)Â½ ) / 2a] andÂ  [(-b – (4ac)Â½) / 2a].

```print("The Roots are {0} and {1}" .format(root1, root2))
```

Finally, the result is displayed on the screen using print() function.

## Conclusion

I hope after going through this post, you understand how to solve a quadratic equation using Python Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.codingbroz.com/python-program-to-solve-quadratic-equation/", "fetch_time": 1721460058000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-30/segments/1720763515020.57/warc/CC-MAIN-20240720052626-20240720082626-00440.warc.gz", "warc_record_offset": 620409583, "warc_record_length": 56794, "token_count": 563, "char_count": 1961, "score": 3.59375, "int_score": 4, "crawl": "CC-MAIN-2024-30", "snapshot_type": "latest", "language": "en", "language_score": 0.7044165134429932, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00008-of-00064.parquet"}