Python Program to Compute a Polynomial Equation

This is a Python Program to compute a polynomial equation given that the coefficients of the polynomial are stored in the list.

Problem Description

The program takes the coefficients of the polynomial equation and the value of x and gives the value of the polynomial.

Problem Solution

1. Import the math module.
2. Take in the coefficients of the polynomial equation and store it in a list.
3. Take in the value of x.
4. Use a for loop and while loop to compute the value of the polynomial expression for the first three terms and store it in a sum variable.
5. Add the fourth term to the sum variable.
6. Print the computed value.
7. Exit.

Program/Source Code

Here is source code of the Python Program to compute a polynomial equation given that the coefficients of the polynomial are stored in a list. The program output is also shown below.

import math
print("Enter the coefficients of the form ax^3 + bx^2 + cx + d")
lst=[]
for i in range(0,4):
    a=int(input("Enter coefficient:"))
    lst.append(a)
x=int(input("Enter the value of x:"))
sum1=0
j=3
for i in range(0,3):
    while(j>0):
        sum1=sum1+(lst[i]*math.pow(x,j))
        break
    j=j-1
sum1=sum1+lst[3]
print("The value of the polynomial is:",sum1)
Program Explanation

1. The math module is imported.
2. User must enter the coefficients of the polynomial which is stored in a list.
3. User must also enter the value of x.
4. The value of i ranges from 0 to 2 using the for loop which is used to access the coefficients in the list.
5. The value of j ranges from 3 to 1, which is used to determine the power for the value of x.
6. The value of the first three terms is computed this way.
7. The last term is added to the final sum.
8. The final computed value is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter the coefficients of the form ax^3 + bx^2 + cx + d
Enter coefficient:3
Enter coefficient:4
Enter coefficient:5
Enter coefficient:6
Enter the value of x:2
The value of the polynomial is: 56.0
 
Case 2:
Enter the coefficients of the form ax^3 + bx^2 + cx + d
Enter coefficient:2
Enter coefficient:5
Enter coefficient:6
Enter coefficient:3
Enter the value of x:1
The value of the polynomial is: 16.0

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.