In this tutorial, you will find the Python 3 program to solve a quadratic equation. To understand this example, you should have the basic knowledge of Python programming concepts such as variables, data types, and operators.
If you are looking for some good books to learn Python, then do check out our recommended list.
Also Read: Sending Emails Using Python With Image And PDF Attachments
Python 3 Program To Solve A Quadratic Equation
Formula to calculate a quadratic equation = ax² + bx + c = 0, where a, b and c are real numbers and a ≠ 0. In the Python code below, users will have to enter the values of a, b, and c and then the program will output the solutions of the quadratic equation.
Source Code
import cmath # import complex math module a = float(input("Enter the value of a= ")) b = float(input("Enter the value of b= ")) c = float(input("Enter the value of c= ")) d = (b**2) - (4*a*c) ## calculate the discriminant sol1 = (-b-cmath.sqrt(d))/(2*a) # Calculating the first solution sol2 = (-b+cmath.sqrt(d))/(2*a) # Calculating the second solution print('The solution of your quadratic equation are {0} and {1}'.format(sol1,sol2))
Now, let’s calculate the roots of an equation x2+5x+6 = 0.
Here, a=1, b=5, and c=6.
Run The Program
Output
Also Check:
- Python 3 Program To Add Two Matrices.
- Python 3 Program to Check Armstrong Number.
- Python 3 Program to Find the Sum of Natural Numbers.
- Python 3 Program To Find The Factorial Of A Number.
- Python 3 Program to Find Largest Among Three Numbers.
- Python 3 Program To Add Two Numbers.
- Python 3 Program to Find the Square Root of A Number.
- Python 3 Program to Calculate The Area of a Triangle.
- Python 3 Program To Convert Celsius To Fahrenheit.
- Python 3 Program To Check If Number Is Positive Or Negative.
Hello Friends! I am Himanshu, a hobbyist programmer, tech enthusiast, and digital content creator.
With CodeItBro, my mission is to promote coding and help people from non-tech backgrounds to learn this modern-age skill!