Sum of Series 1/1 + 1/2 + … + 1/N

Compute the sum of the harmonic series up to N terms.

PythonBeginner
Python
# Program to compute sum of series 1/1 + 1/2 + ... + 1/N

n = int(input("Enter a positive integer N: "))

if n <= 0:
    print("Please enter a positive integer.")
else:
    total = 0.0
    for i in range(1, n + 1):
        total += 1 / i
    print(f"Sum of series up to 1/{n} is {total}")

Output

Enter a positive integer N: 5
Sum of series up to 1/5 is 2.283333333333333

We add each reciprocal 1/i in a loop from 1 to N, accumulating the sum in a float.