Python program to solve quadratic Equation

Python program to solve quadratic Equation. This program is for the beginners , class 11, 12 and B Tech Computer Science (CS) students. It checks the user Python library familiarization, Python simple syntax and mathematical knowledge. Download source code.

Aim: Write a Program to solve quadratic equation in Python.

How does the program works:

  • First quadratic variable are captured as a,b,c variable of the quadratic equation.
  • After that mathematical equation is calculated.
  • Finally the roots of quadratic equation is shown

What is Quadratic equation ?

Quadratic equation is made from a Latin term “quadrates” which means square. It is a special type of equation having the form of: ax2+bx+c=0.
Here, “x” is unknown which we have to find and “a”, “b”, “c” specifies the numbers such that “a” is not equal to 0. If a = 0 then the equation becomes liner not quadratic anymore.
In the equation, a, b and c are called coefficients.

Source code of program:

# import complex math module  
import cmath  
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('The solution are {0} and {1}'.format(sol1,sol2))  

Click below button to Download Source Code

Source code Explanation :

  • In the first line, we have imported the cmath module.
  • We have defined three variables which are coefficient of the equation named a, b, and c which takes input from the user.
  • Then, we calculated the discriminant using the formula.
  • Using the cmath.sqrt() method, we have calculated two solutions and printed the result.

Output:

Python program to solve quadratic Equation

Output Explaination:

ax2 +bx+c is the quadratic equation wiith the variable a,b,c
Coeficient a,b,c is captured from the user.
Then after calculating putting mathematical formula the solution are shown.

Thank you for reading and learning from Python program to solve quadratic Equation. This program is for the beginners , class 11, 12 and B Tech students. It checks the user Python library familiarization, Python simple syntax and mathematical knowledge. There are variety of different programs in the practical file of class 11,12 and B Tech . Below are the links to practical files and other python programs.

Leave a Comment

Your email address will not be published.