# Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n

If inverse of a sequence follows rule of an A.P i.e, Arithmetic progression, then it is said to be in Harmonic Progression.In general, the terms in a harmonic progression can be denoted as : 1/a, 1/(a + d), 1/(a + 2d), 1/(a + 3d) …. 1/(a + nd).

As Nth term of AP is given as ( a + (n – 1)d) .Hence, Nth term of harmonic progression is reciprocal of Nth term of AP, which is : 1/(a + (n – 1)d)

where “a” is the 1st term of AP and “d” is the common difference.

We can use a for loop to find sum.

## C++

`// C++ program to find sum of series ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return sum of ` `// 1/1 + 1/2 + 1/3 + ..+ 1/n ` `class` `gfg ` `{ ` ` ` `public` `: ` `double` `sum(` `int` `n) ` `{ ` ` ` `double` `i, s = 0.0; ` ` ` `for` `(i = 1; i <= n; i++) ` ` ` `s = s + 1/i; ` ` ` `return` `s; ` `} ` `}; ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `gfg g; ` ` ` `int` `n = 5; ` ` ` `cout << ` `"Sum is "` `<< g.sum(n); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by SoM15242. ` |

*chevron_right*

*filter_none*

## C

`// C program to find sum of series ` `#include <stdio.h> ` ` ` `// Function to return sum of 1/1 + 1/2 + 1/3 + ..+ 1/n ` `double` `sum(` `int` `n) ` `{ ` ` ` `double` `i, s = 0.0; ` ` ` `for` `(i = 1; i <= n; i++) ` ` ` `s = s + 1/i; ` ` ` `return` `s; ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `printf` `(` `"Sum is %f"` `, sum(n)); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find sum of series ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return sum of ` ` ` `// 1/1 + 1/2 + 1/3 + ..+ 1/n ` ` ` `static` `double` `sum(` `int` `n) ` ` ` `{ ` ` ` `double` `i, s = ` `0.0` `; ` ` ` `for` `(i = ` `1` `; i <= n; i++) ` ` ` `s = s + ` `1` `/i; ` ` ` `return` `s; ` ` ` `} ` ` ` ` ` ` ` `// Driven Program ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.printf(` `"Sum is %f"` `, sum(n)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find the sum of series ` ` ` `def` `sum` `(n): ` ` ` `i ` `=` `1` ` ` `s ` `=` `0.0` ` ` `for` `i ` `in` `range` `(` `1` `, n` `+` `1` `): ` ` ` `s ` `=` `s ` `+` `1` `/` `i; ` ` ` `return` `s; ` ` ` `# Driver Code ` `n ` `=` `5` `print` `(` `"Sum is"` `, ` `round` `(` `sum` `(n), ` `6` `)) ` ` ` `# This code is contributed by Chinmoy Lenka ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find sum of series ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return sum of ` ` ` `// 1/1 + 1/2 + 1/3 + ..+ 1/n ` ` ` `static` `float` `sum(` `int` `n) ` ` ` `{ ` ` ` `double` `i, s = 0.0; ` ` ` ` ` `for` `(i = 1; i <= n; i++) ` ` ` `s = s + 1/i; ` ` ` ` ` `return` `(` `float` `)s; ` ` ` `} ` ` ` ` ` ` ` `// Driven Program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `Console.WriteLine(` `"Sum is "` ` ` `+ sum(n)); ` ` ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum of series ` ` ` `// Function to return sum of ` `// 1/1 + 1/2 + 1/3 + ..+ 1/n ` `function` `sum( ` `$n` `) ` `{ ` ` ` `$i` `; ` ` ` `$s` `= 0.0; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$s` `= ` `$s` `+ 1 / ` `$i` `; ` ` ` `return` `$s` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 5; ` ` ` `echo` `(` `"Sum is "` `); ` ` ` `echo` `(sum(` `$n` `)); ` ` ` `//This code is contributed by vt_m ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2.283333

## Recommended Posts:

- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find Nth term of the series 3 , 5 , 21 , 51 , 95 , ...
- Program to find Nth term of the series 3, 12, 29, 54, 87, ...
- Program to find Nth term of the series 3, 6, 18, 24, ...
- Program to find Nth term of the series 2, 4, 3, 4, 15...
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....