Harmonic series is inverse of a arithmetic 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 a common difference.
Method #1: Simple approach
C
#include <stdio.h>
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;
}
|
Java
import java.io.*;
class GFG {
static double sum(int n)
{
double i, s = 0.0;
for (i = 1; i <= n; i++)
s = s + 1/i;
return s;
}
public static void main(String args[])
{
int n = 5;
System.out.printf("Sum is %f", sum(n));
}
}
|
Python3
def sum(n):
i = 1
s = 0.0
for i in range(1, n+1):
s = s + 1/i;
return s;
n = 5
print("Sum is", round(sum(n), 6))
|
C#
using System;
class GFG {
static float sum(int n)
{
double i, s = 0.0;
for (i = 1; i <= n; i++)
s = s + 1/i;
return (float)s;
}
public static void Main()
{
int n = 5;
Console.WriteLine("Sum is "
+ sum(n));
}
}
|
PHP
<?php
function sum( $n)
{
$i;
$s = 0.0;
for ($i = 1; $i <= $n; $i++)
$s = $s + 1 / $i;
return $s;
}
$n = 5;
echo("Sum is ");
echo(sum($n));
?>
|
Method #2: Using recursion
C++
#include<bits/stdc++.h>
using namespace std;
float sum(float n)
{
if (n < 2)
return 1;
else
return 1 / n + (sum(n - 1));
}
int main()
{
cout << (sum(8)) << endl;
cout << (sum(10)) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
float sum(float n)
{
if (n < 2)
return 1;
else
return 1 / n + (sum(n - 1));
}
public static void main(String args[])
{
GFG g = new GFG();
System.out.println(g.sum(8));
System.out.print(g.sum(10));
}
}
|
Python 3
def sum(n):
if n < 2:
return 1
else:
return 1 / n + (sum(n - 1))
print(sum(8))
print(sum(10))
|
C#
using System;
class GFG
{
static float sum(float n)
{
if (n < 2)
return 1;
else
return 1 / n + (sum(n - 1));
}
public static void Main()
{
Console.WriteLine(sum(8));
Console.WriteLine(sum(10));
}
}
|
PHP
<?php
function sum($n)
{
if ($n < 2)
return 1;
else
return 1 / $n + (sum($n - 1));
}
echo sum(8) . "\n";
echo sum(10);
?>
|
Output:
2.7178571428571425
2.9289682539682538
Recommended Posts:
- Program to find the Nth Harmonic Number
- Program for harmonic mean of numbers
- Find Harmonic mean using Arithmetic mean and Geometric mean
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + 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 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- 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 Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,...
- Program to find N-th term of series 1, 2, 11, 12, 21….
- Program to find the Nth number of the series 2, 10, 24, 44, 70.....

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.