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
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 – 1000 Python Programs.

If you wish to look at all Python Programming examples, go to 1000 Python Programs.

Free 30-Day Java Certification Bootcamp is Live. Join Now!

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.