KnowledgeBoat Logo
|

Informatics Practices

WAP to print the sum of the series 1 - x1/2! + x2/3! - x3/4! …………… xn/(n + 1)! — exponential series.

Python Control Flow

7 Likes

Answer

x = int(input("Enter x: "))
n = int(input("Enter n: "))
sum_series = 1  
sign = -1 

for i in range(1, n + 1):
    fact = 1
    for j in range(1, i + 2):
        fact *= j
    
    term = sign * (x ** i) / fact 
    sum_series += term
    sign *= -1 

print("Sum =", sum_series)

Output

Enter x: 2
Enter n: 3
Sum = 0.3333333333333333

Answered By

2 Likes


Related Questions