Open In App

Program to find sum of harmonic series

Last Updated : 27 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program to find sum of harmonic series
#include<bits/stdc++.h>
using namespace std;
  
// Function to return sum of harmonic series
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()
{
    int n = 5;
     
    cout << "Sum is " << sum(n);
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10


C




// C program to find sum of harmonic series
#include <stdio.h>
 
// Function to return sum of harmonic series
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




// Java Program to find sum of harmonic series
import java.io.*;
 
class GFG {
     
    // Function to return sum of
    // harmonic series
    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));       
    }
}


Python3




# Python program to find the sum of harmonic 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))


C#




// C# Program to find sum of harmonic series
using System;
 
class GFG {
     
    // Function to return sum of
    // harmonic series
    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));       
    }
}


PHP




<?php
// PHP program to find sum of harmonic series
 
// Function to return sum of
// harmonic series
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));
     
?>


Javascript




<script>
// JavaScript program to find sum of harmonic series
 
// Function to return sum of harmonic series
function sum(n)
{
let i, s = 0.0;
for(i = 1; i <= n; i++)
    s = s + 1 / i;
         
return s;
}
 
// Driver code
let n = 5;
document.write("Sum is " + sum(n));
 
// This code is contributed by Surbhi Tyagi.
</script>


Output: 

Sum is 2.283333

 

Time Complexity : O(n) ,as we are traversing once in array.

Auxiliary Space : O(1) ,no extra space needed.

Method #2: Using recursion 

C++




// CPP program to find sum of
// harmonic series using recursion
#include<bits/stdc++.h>
using namespace std;
 
float sum(float n)
{
    // Base condition
    if (n < 2)
        return 1;
 
    else
        return 1 / n + (sum(n - 1));
}
 
// Driven Code
int main()
{
    cout << (sum(8)) << endl;
    cout << (sum(10)) << endl;
    return 0;
}
 
// This code is contributed by
// Shashank_Sharma


Java




// Java program to find sum of
// harmonic series using recursion
import java.io.*;
 
class GFG
{
 
float sum(float n)
{
    // Base condition
    if (n < 2)
        return 1;
 
    else
        return 1 / n + (sum(n - 1));
}
 
// Driven Code
public static void main(String args[])
{
  GFG g = new GFG();
  System.out.println(g.sum(8));
  System.out.print(g.sum(10));
}
}
 
// This code is contributed by Shivi_Aggarwal


Python3




# Python program to find sum of
# harmonic series using recursion
 
def sum(n):
 
    # Base condition
    if n < 2:
        return 1
 
    else:
        return 1 / n + (sum(n - 1))
         
print(sum(8))
print(sum(10))


C#




//C# program to find sum of
// harmonic series using recursion
using System;
 
class GFG
{
 
static float sum(float n)
{
    // Base condition
    if (n < 2)
        return 1;
 
    else
        return 1 / n + (sum(n - 1));
}
 
// Driven Code
public static void Main()
{
    Console.WriteLine(sum(8));
    Console.WriteLine(sum(10));
}
}
 
// This code is contributed by shs..


PHP




<?php
// PHP program to find sum of
// harmonic series using recursion
 
function sum($n)
{
 
    // Base condition
    if ($n < 2)
        return 1;
 
    else
        return 1 / $n + (sum($n - 1));
}
 
// Driver Code
echo sum(8) . "\n";
echo sum(10);
 
// This code is contributed by Ryuga
?>


Javascript




<script>
 
// Javascript program to find sum of
// harmonic series using recursion
function sum(n)
{
     
    // Base condition
    if (n < 2)
    {
        return 1
    }
    else
    {
        return 1 / n + (sum(n - 1))
    }
}
  
// Driver code
document.write(sum(8));
document.write("<br>");
document.write(sum(10));
 
// This code is contributed by bunnyram19
  
</script>


Output: 

2.7178571428571425
2.9289682539682538

 

Time Complexity : O(n), as we are recursing for n times.

Auxiliary Space : O(n), due to recursive stack space, since n extra space has been taken.



Similar Reads

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 The idea is to traverse from H1 and then consecutively keep finding H2 from H1,
6 min read
Harmonic progression Sum
Given the first element of the progression 'a', common difference between the element 'd' and number of terms in the progression 'n', where [Tex]n, d, a \in [1, 100000] [/Tex]. The task is to generate harmonic progression using the above set of information.Examples: Input : a = 12, d = 12, n = 5 Output : Harmonic Progression : 1/12 1/24 1/36 1/48 1
10 min read
Program for harmonic mean of numbers
Given an array of elements, find harmonic mean of numbers. Examples: Input : arr[] = {2.0, 1.0} Output : 1.3333 Harmonic mean = 2/(1/2.0 + 1/1.0) = (2 * 2)/3 = 1.333 Input : arr[] = {13.5, 14.5, 14.8, 15.2, 16.1} Output : 14.7707 Harmonic mean is used when average of rates is required, below is the formula.Harmonic mean of n numbers x1, x2, x3, . .
8 min read
Find Harmonic mean using Arithmetic mean and Geometric mean
Given two numbers, first calculate arithmetic mean and geometric mean of these two numbers. Using the arithmetic mean and geometric mean so calculated, find the harmonic mean between the two numbers. Examples: Input : a = 2 b = 4 Output : 2.666 Input : a = 5 b = 15 Output : 7.500 Arithmetic Mean: Arithmetic Mean 'AM' between two numbers a and b is
5 min read
Find a sequence of distinct integers whose Harmonic Mean is N
Given an integer N, the task is to find a sequence of distinct integers whose Harmonic Mean is N itself. In case such a sequence doesn't exist, print -1. Examples: Input: N = 5Output: {3, 4, 5, 6, 20} Explanation: Given sequence is the correct answer because 5/(1/3​+1/4+1/5​+1/6​+1/20)​=5. Note that there are other possible sequences as well. For e
6 min read
Leibniz harmonic triangle
The Leibniz harmonic triangle is a triangular arrangement of unit fractions in which the outermost diagonals consist of the reciprocals of the row numbers and each inner cell is the cell diagonally above and to the left minus the cell to the left. To put it algebraically, L(r, 1) = 1/r, where r is the number of the row, starting from 1, and c is th
7 min read
Harmonic Progression
A sequence of numbers is called a Harmonic progression if the reciprocal of the terms are in AP. In simple terms, a, b, c, d, e, f are in HP if 1/a, 1/b, 1/c, 1/d, 1/e, 1/f are in AP. For example, 1/a, 1/(a+d), 1/(a+2d), and so on are in HP because a, a + d, a + 2d are in AP. Fact about Harmonic Progression : In order to solve a problem on Harmonic
7 min read
Summation of floor of harmonic progression
Given an integer N, the task is to find the summation of the harmonic series [Tex]\sum_{i=1}^{n} \lfloor{n/i}\rfloor [/Tex]. Examples: Input: N = 5 Output: 10 floor(3/1) + floor(3/2) + floor(3/3) = 3 + 1 + 1 = 5Input: N = 20 Output: 66 Naive approach: Run a loop from 1 to N and find the summation of the floor values of N / i. Time complexity of thi
4 min read
Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
Given a binary tree, the task is to check whether the nodes in this tree form an arithmetic progression, geometric progression or harmonic progression.Examples: Input: 4 / \ 2 16 / \ / \ 1 8 64 32 Output: Geometric Progression Explanation: The nodes of the binary tree can be used to form a Geometric Progression as follows - {1, 2, 4, 8, 16, 32, 64}
17 min read
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
4 min read
Practice Tags :