{"id":6855,"date":"2023-10-31T17:17:06","date_gmt":"2023-10-31T11:47:06","guid":{"rendered":"https:\/\/python-programs.com\/?p=6855"},"modified":"2023-11-10T12:09:36","modified_gmt":"2023-11-10T06:39:36","slug":"python-program-to-solve-quadratic-equation","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-solve-quadratic-equation\/","title":{"rendered":"Python Program to Solve Quadratic Equation"},"content":{"rendered":"
Explore more instances related to python concepts from Python Programming Examples<\/a> Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n Quadratics or quadratic equations are polynomial equations of the second degree, which means that they contain at least one squared word.<\/p>\n ax2 + bx + c =\u00a00<\/p>\n where x is an unknown variable and the numerical coefficients a , b , c.<\/p>\n Based on the value of discriminant there are three types of roots for Quadratic Equation<\/p>\n Where sqrt is square root.<\/p>\n i)Real and distinct roots<\/strong><\/p>\n When the Value of discriminant is greater than 0 then there exist two distinct roots for the quadratic equation<\/p>\n which can be calculated using the above roots formula.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n ii)Real and equal roots<\/strong><\/p>\n When the Value of discriminant is equal to 0 then there exist two equal roots for the quadratic equation .<\/p>\n which can be calculated using the above roots formula.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n iii)Complex roots<\/strong><\/p>\n When the Value of discriminant is greater than 0 then there exist two complex roots for the quadratic equation .<\/p>\n which can be calculated using the above roots formula.<\/p>\n Examples:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Solve Quadratic Equation using Python Quadratic Equation Discriminant value Calculating roots of Quadratic Equation Types of roots Approach Implementation Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language. 1)Quadratic Equation Quadratics or quadratic equations are polynomial equations of the …<\/p>\n1)Quadratic Equation<\/h3>\n
2)Discriminant value<\/h3>\n
Discriminant = b ^ 2 - 4 * a *c<\/pre>\n
3)Calculating roots of Quadratic Equation<\/h3>\n
roots = ( -b + sqrt(b ^ 2 - 4 * a *c) ) \/ (2 * a)\u00a0 \u00a0, ( -b - sqrt(b ^ 2 - 4 * a *c) ) \/ (2 * a)<\/pre>\n
4)Types of roots<\/h3>\n
a = 2\r\nb = -7\r\nc = 6<\/pre>\n
The two distinct roots are : \r\n(2+0j)\r\n(1.5+0j)<\/pre>\n
a = 1\r\nb = -4\r\nc = 4<\/pre>\n
The two equal roots are : \r\n2.0 2.0<\/pre>\n
a = 5\r\nb = 2\r\nc = 3<\/pre>\n
There exists two complex roots:\r\n(-1+1.7320508075688772j)\r\n(-1-1.7320508075688772j)<\/pre>\n
5)Approach<\/h3>\n
\n
6)Implementation:<\/h3>\n
# importing cmath\r\nimport cmath\r\n# given a,b,c values\r\na = 2\r\nb = -7\r\nc = 6\r\ndiscriminant = (b**2) - (4*a*c)\r\n# checking if the value of discriminant is greater than 0\r\nif(discriminant > 0):\r\n # here exist the two distinct roots and we print them\r\n # calculating the roots\r\n root1 = (-b+discriminant) \/ (2 * a)\r\n root2 = (-b-discriminant) \/ (2 * a)\r\n # printing the roots\r\n\r\n print(\"The two distinct roots are : \")\r\n print(root1)\r\n print(root2)\r\n# checking if the value of discriminant is equal to 0\r\nelif(discriminant == 0):\r\n # here exist the two equal roots\r\n # calculating single root here discriminant is 0 so we dont need to write full formulae\r\n root = (-b)\/(2*a)\r\n # printing the root\r\n print(\"The two equal roots are : \")\r\n print(root, root)\r\n# else there exists complex roots\r\nelse:\r\n # here exist the two complex roots\r\n # calculating complex roots\r\n realpart = -b\/(2*a)\r\n complexpart = discriminant\/(2*a)*(-1)\r\n # printing the roots\r\n print(\"There exists two complex roots:\")\r\n print(realpart, \"+\", complexpart, \"i\")\r\n print(realpart, \"-\", complexpart, \"i\")\r\n<\/pre>\n
The two distinct roots are : \r\n(2+0j)\r\n(1.5+0j)<\/pre>\n
\n