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

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

Here,we are going to write python program to input the value of x and n and we also will print the sum of series

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

Python program to print sum of given series using if-else

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)/i)
    else:
        sum = sum-((x**i)/i)
print("Sum of Series is :",sum)




Output

Enter the value of x : 2
Enter the value of n : 8
Sum of Series is : 23.314285714285717

Python program to print series without using if-else loop

#without using if else loop
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)/i)
print("Sum of Series is :",sum)

Output

Enter the value of x : 2
Enter the value of n : 8
Sum of Series is : 23.314285714285717
<