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 Python.
Examples:
Example1:
Input:
given coefficient list = [7, 1, 3, 2]
Output:
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
Example2:
Input:
given coefficient list = [3, 9, 1, 2]
Output:
The total value of the given polynomial 3 x^3 +9 x^2 +1 x +2 with the given value of x 6 = 980
Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List
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:
Method #1:Using for and while loop(User Input)
Approach:
- Import the math module.
- Take a empty list.
- Loop from 1 to 4 using for loop as there are 4 coefficients terms in the equation.
- Scan the coefficient as user input using int(input()) function.
- Add this to the list using append() function.
- Scan the value of x as user input using int(input()) function.
- To compute the value of the polynomial expression for the first three terms, use a for loop and a while loop and store it in a sum variable.
- To the total variable, add the fourth term.
- The computed value should be printed.
- Exit of program.
Below is the implementation:
import math
# Take a empty list.
coefflist = []
# Loop from 1 to 4 using for loop as there are 4 coefficients terms in the equation.
for t in range(4):
# Scan the coefficient as user input using int(input()) function.
elemen = int(input('Enter some random coefficient ='))
# Add this to the list using append() function.
coefflist.append(elemen)
# Scan the value of x as user input using int(input()) function.
x = int(input('Enter some random value of x ='))
# Taking a variable which stores totalValue and initialize it with 0
totalValue = 0
temp = 3
for k in range(0, 3):
while(temp > 0):
totalValue = totalValue+(coefflist[k]*(x**temp))
# breaking the while looop
break
temp = temp-1
# To the total variable, add the fourth term.
totalValue = totalValue+coefflist[3]
print("The total value of the given polynomial " +
str(coefflist[0])+' x^3 +'+str(coefflist[1])+' x^2 +'+str(coefflist[2])+' x +' +
str(coefflist[3]), 'with the given value of x=', x, 'is', totalValue)
Output:
Enter some random coefficient =1 Enter some random coefficient =9 Enter some random coefficient =3 Enter some random coefficient =5 Enter some random value of x =8 The total value of the given polynomial 1 x^3 +9 x^2 +3 x +5 with the given value of x= 8 is 1117
Explanation:
- The math module has been imported.
- The user must enter the polynomial coefficients, which are saved in a list.
- In addition, the user must input the value of x.
- The for loop, which is used to retrieve the coefficients in the list, changes the value of I from 0 to 2.
- The power for the value of x is determined by the value of j, which varies from 3 to 1.
- This method is used to compute the values of the first three terms.
- The final term is added to the total.
- The final calculated value is printed.
Method #2: Using for and while loop(Static Input)
Approach:
- Import the math module.
- Take in the polynomial equation coefficients as static input in given list.
- Give the value of x as static input.
- To compute the value of the polynomial expression for the first three terms, use a for loop and a while loop and store it in a sum variable.
- To the total variable, add the fourth term.
- The computed value should be printed.
- Exit of program.
Below is the implementation:
import math
# Take in the polynomial equation coefficients as static input in given list.
coefflist = [7, 1, 3, 2]
# Give the value of x as static input.
x = 5
# Taking a variable which stores totalValue and initialize it with 0
totalValue = 0
temp = 3
for k in range(0, 3):
while(temp > 0):
totalValue = totalValue+(coefflist[k]*(x**temp))
# breaking the while looop
break
temp = temp-1
# To the total variable, add the fourth term.
totalValue = totalValue+coefflist[3]
print("The total value of the given polynomial " +
str(coefflist[0])+' x^3 +'+str(coefflist[1])+' x^2 +'+str(coefflist[2])+' x +' +
str(coefflist[3]), 'with the given value of x=', x, 'is', totalValue)
Output:
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
Explanation:
- The for loop, which is used to retrieve the coefficients in the list, changes the value of I from 0 to 2.
- The power for the value of x is determined by the value of j, which varies from 3 to 1.
- This method is used to compute the values of the first three terms.
- The final term is added to the total.
- The final calculated value is printed.
Related Programs:
