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).
Input
The first line of input is an integer
N.
Output
Print the sum rounded upto 2 decimal places.
Explanation
For
N = 5
The 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('Enter integer here: '))
j = 0
for i in range(1,N+1):
j = j + 1/i
print(round(j,2))
Enter integer here: 5
2.28
Enter integer here: 3
1.83
Comments
Leave a comment