Python program to solve 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:
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:
Enter a: 8 Enter b: 5 Enter c: 9 The solution are (-0.3125-1.0135796712641785j) and (-0.3125+1.0135796712641785j)
In the first line, we have imported the cmath module and we have defined three variables 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.
We can get the solution of the quadric equation by using direct formula. Let's understand the following example.
Above formula consists of the following cases.
Enter a:7 Enter b:5 Enter c:2 Complex Roots -0.35714285714285715 + i 5.5677643628300215 -0.35714285714285715 - i 5.5677643628300215
In the above code, we have imported the math module and defined the formula to calculate discriminant. We have then defined findRoots function which take three integer values as arguments. Then we have checked the roots using the if-elif-else statement.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week