In this tutorial you will learn about the Python Program to Solve Quadratic Equation and its application with practical example.
In this tutorial, we will learn to create a Python Program to Solve Quadratic Equation using python Programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Python programming topics:
- Python Operator.
- Basic Input and Output
- Basic Python programming.
- Python if-else statements.
- Python Loop.
what is Quadratic Equations?
The Standard Quadratic Equation looks like this:
Here,
-
- a, b and c are known values.and ‘a‘ cannot be 0.
- ‘x‘ is unknown variable.
The name Quadratic is come from Latin and means “to square” from “quad” means square, because the variable gets squared (like x2).
Quadratic equations are second-degree algebraic expressions and are of form ax2 + bx + c = 0.
Python Program to Solve Quadratic Equation
In this program we will create a program which will solve a Quadratic Equation.First we would declared and initialized the required variables. Next, we would prompt user to input the value. Later we will find solution of Quadratic Equation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# import complex math module in our program import cmath #taking values form user at run time. a = int(input("enter value of a")) b = int(input("enter value of b")) c = int(input("enter value of c")) # calculating the discriminant here d = (b**2) - (4*a*c) # finding two solutions of the quadratic equation. s1 = (-b-cmath.sqrt(d))/(2*a) s2 = (-b+cmath.sqrt(d))/(2*a) # printing the two solutions. print('The solution are {0} and {1}'.format(s1,s2)) |
Output
In our program, we have first declared and initialized a set variables required in the program.
- a =taking value for a.
- b =taking value for b.
- c =taking value for c.
- s1=for solution one.
- s2=for solution two
First of all we declare variable and take three value from user and assign to ‘a’,’b’ and in ‘c’ variable.
After these values we will find the value of discriminant
here we calculated the discriminant and then now we find the two possible solutions of the quadratic equation.
In our program you can change coefficient and real numbers ‘a’,’b’and‘c’ and get different different solution of quadratic equation.