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

04 Nov, 2021
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
Java
Python3
C#
PHP
Javascript
// C++ program to find sum of series#include <iostream>using namespace std; // Function to return sum of// 1/1 + 1/2 + 1/3 + ..+ 1/nclass gfg{     public : double sum(int n){    double i, s = 0.0;    for (i = 1; i <= n; i++)    s = s + 1/i;    return s;}}; // Driver codeint main(){    gfg g;    int n = 5;    cout << "Sum is " << g.sum(n);    return 0;} // This code is contributed by SoM15242.
// C program to find sum of series#include <stdio.h> // Function to return sum of 1/1 + 1/2 + 1/3 + ..+ 1/ndouble 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 Program to find sum of seriesimport 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.
# 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 Coden = 5print("Sum is", round(sum(n), 6)) # This code is contributed by Chinmoy Lenka
// C# Program to find sum of seriesusing 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.
<?php// PHP program to find sum of series // Function to return sum of// 1/1 + 1/2 + 1/3 + ..+ 1/nfunction 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?>
<script>// javascript Program to find sum of series     // Function to return sum of// 1/1 + 1/2 + 1/3 + ..+ 1/nfunction sum(n){  var i, s = 0.0;  for (i = 1; i <= n; i++)      s = s + 1/i;  return s;}    // Driven Programvar n = 5;document.write(sum(n).toFixed(5)); // This code is contributed by Amit Katiyar</script>
Output:  
2.283333
Time Complexity: O(n)
Auxiliary Space: O(1)
 
vt_m
SoumikMondal
amit143katiyar
subhammahato348
harmonic progression
series
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Modulo Operator (%) in C/C++ with Examples
Program to find sum of elements in a given array
Program for factorial of a number
Operators in C / C++
Euclidean algorithms (Basic and Extended)
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java