Write a program to input the value of x and n and print the sum of the following series:

4) x + x2/2! – x3/3! + x4/4! – x5/5! + x6/6!

Python program to print series using if-else loop

#Program to print x + x2/2! - x3/3! ...............
import math
x = int(input("Enter the value of x : "))
n = int(input("Enter the value of n : "))
sum = x
for i in range(2,n+1):
    if(i%2 == 0):
        sum = sum+((x**i)/math.factorial(i))
    else:
        sum = sum-((x**i)/math.factorial(i))
print("Sum of Series is :",sum)


    

Output

Enter the value of x : 3
Enter the value of n : 4
Sum of Series is : 6.375

Python program to print series without using if-else loop

#without using if else loop
import math
x = int(input("Enter the value of x : "))
n = int(input("Enter the value of n : "))
sum = x
for i in range(2,n+1):
      sum = sum + ((-1)**i)* ((x**i)/math.factorial(i))
print("Sum of Series is :",sum)

Enter the value of x : 3
Enter the value of n : 4
Sum of Series is : 6.375
<