Python program to solve Quadratic equation

In this tutorial, we are going to learn how to solve the quadratic equation program in Python.

The standard form of the quadratic equation is ax2+bx+c=0. Where a, b,c are real numbers.

Here ‘x’ is an unknown value that we need to find out and we should give the input values for coefficients a, b, c that should be not equal to 0.

  • Let us consider the simplest flowchart of the quadratic equation which shows the flow of execution of the code.

flowchart of the quadratic equation

Solving the quadratic equation by using the Python program

First, we need to have the basic knowledge of Python programming like Python datatypes, operators to be used, input and output functions, libraries, etc.

Now look at our code below to do solve the quadratic equation:

import cmath
a=float(input("enter value of a:"))
b=float(input("enter value of b:"))
c=float(input("enter value of c:"))
d=(b**2)-(4*a*c)
s1=(-b+cmath.sqrt(d))/(2*a)
s2=(-b-cmath.sqrt(d))/(2*a)
print("solutions are {0} and {1}".format(s1,s2))

Here we imported the standard module cmath to perform the mathematical calculations. And we can observe from the code we are passing command-line arguments by using the input function. We can pass integer, floating-point or any other types available in Python.

Output:

enter value of a:1
enter value of b:5
enter value of c:6
solutions are (-2+0j) and (-3+0j)

This program allows us to enter the values of coefficients a, b, c and it calculates the equation using the standard formula to find the roots of the quadratic equation.

Also, read: Plot an angle in Python using matplotlib

Leave a Reply

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