Skip to content
Related Articles

Related Articles

Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + – – – – – – upto n terms.
  • Last Updated : 27 Jul, 2018

Given a positive integer n, find the sum of the series upto n terms.

Examples :

Input : n = 4 
Output : 3.91667
Series : (1 / 1) + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + (1 + 2 + 3 + 4) / (1 * 2 * 3 * 4)
       = 1 / 1 + 3 / 2 + 6 / 6 + 10 / 24
       = 3.91667

Input : n = 6
Output : 4.07083
Series : (1 / 1) + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3)  + (1 + 2 + 3 + 4) / (1 * 2 * 3 * 4) + (1 + 2 + 3 + 4 + 5) / (1 * 2 * 3 * 4 * 5)  + (1 + 2 + 3 + 4 + 5 + 6) / (1 * 2 * 3 * 4 * 5 * 6)
       = 1 / 1 + 3 / 2 + 6 / 6 + 10 / 24 + 15 / 120 + 21 / 720
       = 4.07083

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find sum of series.
#include <bits/stdc++.h>
using namespace std;
  
double sumOfSeries(int n)
{
    double res = 0.0 ;
    int sum = 0, prod = 1;
    for (int i = 1 ; i <= n ; i++)
    {
        sum += i;
        prod *= i;
        res += ((double)sum / prod);
    }
    return res;
}
  
// Driver Code
int main()
{
    int n = 4 ;
    cout << sumOfSeries(n) ;    
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find sum of series.
class GFG 
{
static double sumOfSeries(int n) 
{
    double res = 0.0;
    int sum = 0, prod = 1;
    for (int i = 1; i <= n; i++) {
    sum += i;
    prod *= i;
    res += ((double)sum / prod);
    }
    return res;
}
  
// Driver code
public static void main(String arg[]) {
    int n = 4;
    System.out.println(sumOfSeries(n));
}
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to
# find sum of series.
  
def sumOfSeries(n) :
  
    res = 0.0
    sum = 0
    prod = 1
    for i in range(1, n + 1) :
      
        sum = sum + i
        prod = prod * i
        res = res + (sum / prod)
          
    return res
      
# Driver Code
n = 4
print (round(sumOfSeries(n), 5)) 
  
# This code is contributed by 
# Manish Shaw(manishshaw1)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find sum of series.
using System;
  
class GFG {
  
    static double sumOfSeries(int n)
    {
        double res = 0.0;
        int sum = 0, prod = 1;
        for (int i = 1; i <= n; i++) {
            sum += i;
            prod *= i;
            res += ((double)sum / prod);
        }
        return res;
    }
  
    // Driver code
    public static void Main()
    {
        int n = 4;
        Console.Write(sumOfSeries(n));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find sum of series.
  
function sumOfSeries($n)
{
    $res = 0.0 ;
    $sum = 0; $prod = 1;
    for ($i = 1 ; $i <= $n ; $i++)
    {
        $sum += $i;
        $prod *= $i;
        $res += ((double)$sum / $prod);
    }
    return $res;
}
  
// Driver Code
$n = 4 ;
echo(sumOfSeries($n)) ; 
  
// This code is contributed by Ajit.
?>

chevron_right



Output :

3.91667



My Personal Notes arrow_drop_up
Recommended Articles
Page :