44 - Write a program in Python to input the value of x and n and print the sum of the following series:
1 - x + x2 - x3 +x4 + -------- xn
x=int(input("Enter the value of x: "))
n=int(input("Enter the value of n: "))
sum=0
for i in range(0,n+1):
if i%2==0:
sum=sum+pow(x,i)
else:
sum=sum-pow(x,i)
print("Sum of the series=",sum)
Output:
Enter the value of x: 2
Enter the value of n: 3
Sum of the series= -5
Enter the value of x: 2
Enter the value of n: 4
Sum of the series= 11
No comments:
Post a Comment