Python program to solve quadratic equation

Created with Sketch.

Python program to solve quadratic equation

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 you 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.

Let’s take an example to solve the quadratic equation 8x2 + 16x + 8 = 0

See this example:

  1. # import complex math module
  2. import cmath
  3. a = float(input(‘Enter a: ‘))
  4. b = float(input(‘Enter b: ‘))
  5. c = float(input(‘Enter c: ‘))
  6. # calculate the discriminant
  7. d = (b**2) – (4*a*c)
  8. # find two solutions
  9. sol1 = (-b-cmath.sqrt(d))/(2*a)
  10. sol2 = (-b+cmath.sqrt(d))/(2*a)
  11. print(‘The solution are {0} and {1}’.format(sol1,sol2))

Output:

Python Basic Programs5

 

Leave a Reply

Your email address will not be published. Required fields are marked *