{"id":9555,"date":"2021-09-30T11:30:09","date_gmt":"2021-09-30T06:00:09","guid":{"rendered":"https:\/\/python-programs.com\/?p=9555"},"modified":"2021-11-22T18:35:30","modified_gmt":"2021-11-22T13:05:30","slug":"python-program-to-compute-a-polynomial-equation-given-that-the-coefficients-of-the-polynomial-are-stored-in-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-compute-a-polynomial-equation-given-that-the-coefficients-of-the-polynomial-are-stored-in-a-list\/","title":{"rendered":"Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List"},"content":{"rendered":"
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples<\/a><\/p>\n Given Coefficients of the polynomial and x which are stored in the list, the task is to compute the value of the polynomial equation with given x from the given Coefficients in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to compute the value of the polynomial equation with given x from the given Coefficients in Python some of them are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples Given Coefficients of the polynomial and x which are stored in the list, the task is to compute the value of the polynomial equation with given x from the given Coefficients in …<\/p>\ngiven coefficient list = [7, 1, 3, 2]<\/pre>\n
The total value of the given polynomial 7 x^3 +1 x^2 +3 x +2 with the given value of x=5 is 917<\/pre>\n
given coefficient list = [3, 9, 1, 2]<\/pre>\n
The total value of the given polynomial 3 x^3 +9 x^2 +1 x +2 with the given value of x 6 = 980<\/pre>\n
Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List<\/h2>\n
\n
Method #1:Using for and while loop(User Input)<\/h3>\n
\n
import math\r\n# Take a empty list.\r\ncoefflist = []\r\n# Loop from 1 to 4 using for loop as there are 4 coefficients terms in the equation.\r\nfor t in range(4):\r\n # Scan the coefficient as user input using int(input()) function.\r\n elemen = int(input('Enter some random coefficient ='))\r\n # Add this to the list using append() function.\r\n coefflist.append(elemen)\r\n# Scan the value of x as user input using int(input()) function.\r\nx = int(input('Enter some random value of x ='))\r\n# Taking a variable which stores totalValue and initialize it with 0\r\ntotalValue = 0\r\ntemp = 3\r\nfor k in range(0, 3):\r\n while(temp > 0):\r\n totalValue = totalValue+(coefflist[k]*(x**temp))\r\n # breaking the while looop\r\n break\r\n temp = temp-1\r\n# To the total variable, add the fourth term.\r\ntotalValue = totalValue+coefflist[3]\r\nprint(\"The total value of the given polynomial \" +\r\n str(coefflist[0])+' x^3 +'+str(coefflist[1])+' x^2 +'+str(coefflist[2])+' x +' +\r\n str(coefflist[3]), 'with the given value of x=', x, 'is', totalValue)\r\n<\/pre>\n
Enter some random coefficient =1\r\nEnter some random coefficient =9\r\nEnter some random coefficient =3\r\nEnter some random coefficient =5\r\nEnter some random value of x =8\r\nThe total value of the given polynomial 1 x^3 +9 x^2 +3 x +5 with the given value of x= 8 is 1117<\/pre>\n
\n
Method #2: Using for and while loop(Static Input)<\/h3>\n
\n
import math\r\n# Take in the polynomial equation coefficients as static input in given list.\r\ncoefflist = [7, 1, 3, 2]\r\n# Give the value of x as static input.\r\nx = 5\r\n# Taking a variable which stores totalValue and initialize it with 0\r\ntotalValue = 0\r\ntemp = 3\r\nfor k in range(0, 3):\r\n while(temp > 0):\r\n totalValue = totalValue+(coefflist[k]*(x**temp))\r\n # breaking the while looop\r\n break\r\n temp = temp-1\r\n# To the total variable, add the fourth term.\r\ntotalValue = totalValue+coefflist[3]\r\nprint(\"The total value of the given polynomial \" +\r\n str(coefflist[0])+' x^3 +'+str(coefflist[1])+' x^2 +'+str(coefflist[2])+' x +' +\r\n str(coefflist[3]), 'with the given value of x=', x, 'is', totalValue)\r\n<\/pre>\n
The total value of the given polynomial 7 x^3 +1 x^2 +3 x +2 with the given value of x=5 is 917<\/pre>\n
\n
\n