How to Sum a Polynomial Equation in Python


In this example, we will learn how to calculate the polynomial equation of a + bx + cx^2 + dx^3 in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import math

print("Input the factors of formula a + bx + cx^2 + dx^3: ")
a = []
for i in range(0, 4):
    b = int(input("Enter coefficient: "))
    a.append(b)
x = int(input("Enter the value of x: "))
sum1 = 0
for i in range(0, 4):
    sum1 = sum1 + (a[i] * math.pow(x, i))
print("The value of the polynomial is: ", sum1)

Output:

Input the factors of formula a + bx + cx^2 + dx^3: 
Enter coefficient: 1
Enter coefficient: 2
Enter coefficient: 3
Enter coefficient: 4
Enter the value of x: 2
The value of the polynomial is:  49.0

Input the factors of formula a + bx + cx^2 + dx^3: 
Enter coefficient: 3
Enter coefficient: 5
Enter coefficient: 8
Enter coefficient: 9
Enter the value of x: 1
The value of the polynomial is:  25.0

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement