This Python program reads the number of terms in a polynomial (up to 100), stores the coefficients in an array, then reads the value of x and computes:
p = a₀xⁿ + a₁xⁿ⁻¹ + ... + aₙ
def evaluate_polynomial(coeffs: list[float], x: float) -> float:
n = len(coeffs)
p = 0.0
for i in range(n):
power = n - 1 - i
p += coeffs[i] * (x ** power)
return p
# Read number of terms
n = int(input("Enter number of polynomial terms (max 100): "))
if n > 100 or n <= 0:
print("Invalid term count.")
exit()
# Read coefficients
coeffs = []
for i in range(n):
a = float(input(f"Enter coefficient a{i}: "))
coeffs.append(a)
# Read x value
x = float(input("Enter value for x: "))
# Compute and display result
p = evaluate_polynomial(coeffs, x)
print(f"Polynomial value is: {p:.4f}")
Term count: 4
Coefficients: 2 3 -1 5
x = 2
Calculation: 2×2³ + 3×2² + (-1)×2¹ + 5 = 31
Output: Polynomial value is: 31.0000