﻿ Python Data Structures and Algorithms - Calculate the harmonic sum - w3resource

# Python Data Structures and Algorithms - Recursion: Calculate the harmonic sum

## Python Recursion: Exercise-8 with Solution

Write a Python program to calculate the sum of harmonic series upto n terms.

Note: The harmonic sum is the sum of reciprocals of the positive integers.

Example:

Sample Solution:-

Python Code:

``````# Define a function named harmonic_sum that calculates the harmonic sum up to 'n' terms
def harmonic_sum(n):
# Check if 'n' is less than 2 (base case for the harmonic sum)
if n < 2:
# If 'n' is less than 2, return 1 (base case value for the harmonic sum)
return 1
else:
# If 'n' is greater than or equal to 2, calculate the reciprocal of 'n'
# and add it to the result of recursively calling the harmonic_sum function with 'n - 1'
return 1 / n + harmonic_sum(n - 1)

# Print the result of calling the harmonic_sum function with the input value 7
print(harmonic_sum(7))

# Print the result of calling the harmonic_sum function with the input value 4
print(harmonic_sum(4))
```
```

Sample Output:

```2.5928571428571425
2.083333333333333
```

Flowchart:

Python Code Editor: