In this article, we have provided a python source code that is capable of finding the roots for a quadratic equation if the user provides the program with coefficient values of x 2 , x and constant.
Prerequisite topics to create this program
- Python Input, Output
- Python Data types
- Python Operators
- python modules
Steps
- ask the user to enter the coefficient values of a, b and c real numbers.
- calculate the discriminant value by suing the formula d = (b**2) - (4*a*c)
- At last, using the quadratic formula, we will calculate the two possible roots.
- A quadratic equation could have imaginary root values so for that here we will use the cmath module to perform the sqrt() function.
General Quadratic Equation
ax2 + bx + c = 0, where a, b and c are real numbers and a ? 0
Python Program to Solve Quadratic Equation Code
Input
Output
Behind the Code
Here the cmath.sqrt() method is used to find the square root of the discriminant value d. For some equation the d could be negative and when it happen the cmath.sqrt() function calculate the square root of the value in complex number.
People are also reading:
- Program in C++ and Python to find the sum of the series x+ x2/2 +x3/3 + ……. +xn/n
- Python Program to Remove Punctuations From a String
- Program in C++ & Python for the Union of Two Arrays
- Python Program to Find the Largest Among Three Numbers
- Program in C++ & Python to Reverse a String
- Python Program to Find Sum of Natural Numbers Using Recursion
- Program in C++ and python to find the root of quadratic equation
- Most Asked Pattern Programs in C
- Program in C++ & Python & sort an Array Using Bubble Sort
- Python Program to Find LCM
Leave a Comment on this Post