Print Harmonic Series

Print harmonic series: 1 + 1/2 + 1/3 + ... + 1/n.

IntermediatePhase 2 - Level 3: Mathematical & Logical PatternsExample 11 of 13
print-harmonic-series.py
1# Take n
2n = int(input("Enter n: "))
3
4# Print harmonic series
5print("Harmonic series:")
6for i in range(1, n + 1):
7 print(f"1/{i}", end="")
8 if i < n:
9 print(" + ", end="")
10print()

Output

Enter n: 5
Harmonic series:
1/1 + 1/2 + 1/3 + 1/4 + 1/5

What's going on

Print fractions in harmonic series format.

Key Concepts:

Loop from 1 to n
Print 1/i for each term
Add + between terms