Quadratic Equation in Python
Hello Everyone,
In this post we gonna discuss about how to solve quadratic equation in Python..
Before that in the last post we discuss Interactive Mode in Python..
Here is the link,http://vitaminpy.kittu.xyz/2017/12/additionsubtractionmultiplication.html
In this post we are going to learn how to do Python programming in Script Mode.
Ok why are you waiting,lets move into program.
Program:
# Solve the quadratic equation ax**2 + bx + c = 0
import cmath
# To take coefficient input from the users
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter 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("solution1",sol1)
print("solution2",sol2)
In this post we gonna discuss about how to solve quadratic equation in Python..
Before that in the last post we discuss Interactive Mode in Python..
Here is the link,http://vitaminpy.kittu.xyz/2017/12/additionsubtractionmultiplication.html
In this post we are going to learn how to do Python programming in Script Mode.
Ok why are you waiting,lets move into program.
Program:
# Solve the quadratic equation ax**2 + bx + c = 0
import cmath
# To take coefficient input from the users
a = float(input('Enter a: '))
b = float(input('Enter b: '))
c = float(input('Enter 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("solution1",sol1)
print("solution2",sol2)
Here in the first line we import the cmath package which includes all mathematical functions like math.h in c.
The input function is used to take input from the user and and its type is float.
When we calculating the d value we use symbol of two starts(**),it defines exponentiation in Python,it is a special operator compare to other languages.
1.And all the above program is typed in a textfile with .py extension and it is run by using Py shell.
see the image below for input and output of the above program.
2.After step 1,we have to run the module by clicking on Run Module or by clicking F5,Then it will prompt a new window with Py shell like below,
3.Now we have to the user input and the program will works out
Do you want learn the same program in Java Then here is the link,
Stay tuned..
Please subscribe our blog for more interesting topics..
Thanks


Comments
Post a Comment