Sum of N terms in Harmonic series:
Given integer N as input, write a program to display the sum of the first N terms in harmonic series. The series is: 1 + 1/2 + 1/3 + 1/4 + 1/5 ... + 1/N (N terms).
The first line of input is an integer N.
Output
Print the sum rounded up to 2 decimal places.
For
N = 5The sum of first 5 terms in harmonic series is 1 + 1/2 + 1/3 + 1/4 + 1/5
So, the output should be
2.28.
Sample Input 1
5
Sample Output 1
2.28
Sample Input 2
3
Sample Output 2
1.83
n = int(input())
sum = 0
for i in range(1, n + 1):
sum += 1 / i
print("%.2f" % sum)
Comments
please slove above question
Leave a comment