How to Sum (1 + 1/2 + 1/3 + ….. + 1/N) in Python


In this example we will show how to calculate the sum of a series (1 + 1/2 + 1/3 + … + 1/N) in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input("Enter the number of terms: "))
sum1 = 0
while n > 0:
    sum1 = sum1 + (1 / n)
    n = n - 1
print("The sum of series is", round(sum1, 2))

Output:

Enter the number of terms: 5
The sum of series is 2.28

Enter the number of terms: 10
The sum of series is 2.93
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments