# Program to find the Nth Harmonic Number

Given a number N. The task is to find the Nth Harmonic Number.

Let the nth harmonic number be Hn.

The harmonic series is as follows:

H1 = 1
H2 = H1 + 1/2
H3 = H2 + 1/3
H4 = H3 + 1/4
.
.
.
Hn = Hn-1 + 1/n

Examples:

```Input : N = 5
Output : 2.45

Input : N = 9
Output : 2.71786
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to traverse from H1 and then consecutively keep finding H2 from H1, H3 from H2 ….. and so on.

Below is the program to find N-th Harmonic Number:

## C++

 `// CPP program to find N-th Harmonic Number ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find N-th Harmonic Number ` `double` `nthHarmonic(``int` `N) ` `{ ` `    ``// H1 = 1 ` `    ``float` `harmonic = 1.00; ` ` `  `    ``// loop to apply the forumula  ` `    ``// Hn = H1 + H2 + H3 ... + Hn-1 + Hn-1 + 1/n ` `    ``for` `(``int` `i = 2; i <= N; i++) { ` `        ``harmonic += (``float``)1 / i; ` `    ``} ` ` `  `    ``return` `harmonic; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `N = 8; ` `     `  `    ``cout<

## Java

 `// Java program to find N-th Harmonic Number ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `     `  `// Function to find N-th Harmonic Number ` `static` `double` `nthHarmonic(``int` `N) ` `{ ` `    ``// H1 = 1 ` `    ``float` `harmonic = ``1``; ` ` `  `    ``// loop to apply the forumula  ` `    ``// Hn = H1 + H2 + H3 ... + Hn-1 + Hn-1 + 1/n ` `    ``for` `(``int` `i = ``2``; i <= N; i++) { ` `        ``harmonic += (``float``)``1` `/ i; ` `    ``} ` ` `  `    ``return` `harmonic; ` `} ` ` `  `// Driver Code ` ` `  `    ``public` `static` `void` `main (String[] args) { ` `            ``int` `N = ``8``; ` `     `  `    ``System.out.print(nthHarmonic(N)); ` `     `  `    ``} ` `} ` `// This code is contributed  ` `// by ajit `

## Python 3

 `# Python3 program to find  ` `# N-th Harmonic Number ` ` `  `# Function to find N-th Harmonic Number  ` `def` `nthHarmonic(N) : ` ` `  `    ``# H1 = 1  ` `    ``harmonic ``=` `1.00` ` `  `    ``# loop to apply the forumula  ` `    ``# Hn = H1 + H2 + H3 ... +  ` `    ``# Hn-1 + Hn-1 + 1/n  ` `    ``for` `i ``in` `range``(``2``, N ``+` `1``) : ` `        ``harmonic ``+``=` `1` `/` `i ` ` `  `    ``return` `harmonic ` `     `  `# Driver code      ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``N ``=` `8` `    ``print``(``round``(nthHarmonic(N),``5``)) ` ` `  `# This code is contributed by ANKITRAI1 `

## C#

 `// C# program to find N-th Harmonic Number ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `// Function to find N-th Harmonic Number ` `static` `double` `nthHarmonic(``int` `N) ` `{ ` `    ``// H1 = 1 ` `    ``float` `harmonic = 1; ` ` `  `    ``// loop to apply the forumula  ` `    ``// Hn = H1 + H2 + H3 ... +  ` `    ``// Hn-1 + Hn-1 + 1/n ` `    ``for` `(``int` `i = 2; i <= N; i++) ` `    ``{ ` `        ``harmonic += (``float``)1 / i; ` `    ``} ` ` `  `    ``return` `harmonic; ` `} ` ` `  `// Driver Code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `N = 8; ` `     `  `    ``Console.Write(nthHarmonic(N)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Raj `

## PHP

 ` `

Output:

```2.71786
```

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.