Programming Examples
Python Function to Compute the Sum of n Terms of the Following Series
Write a Function to Compute the Sum of n Terms of the Following Series.
Series: X + X^4 / 4! + X^6 / 6! + X^8 / 8! + ... for any positive integer value of X.
import math
def series_sum(x, n):
total = x # First term is X
power = 4 # Starting power for next term
for i in range(1, n):
total += (x ** power) / math.factorial(power)
power += 2 # Increase power by 2 each time
return total
x = int(input("Enter value of X: "))
n = int(input("Enter number of terms: "))
result = series_sum(x, n)
print("Sum of the series:", result)
Output/ Explanation: