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

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

BeginnerTopic: Loop Programs
Back

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

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# 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

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

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

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents