Compute the sum up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.

Source Code

n = int(input("Enter the value of n: "))
sum = 0
sign = -1
for i in range(1,n+1):
    sign = sign * -1
    sum = sum + sign * 1/i
print("Sum of series for first",n,"terms is",sum)

Output

Enter the value of n: 10
Sum of series for first 10 terms is 0.6456349206349207