Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + – – – – – – upto n terms.

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

 `// CPP program to find sum of series. ``#include ``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; ``} `

Java

 `// 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. `

Python3

 `# 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) `

C#

 `// 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. `

PHP

 ` `

Javascript

 ``

Output :

`3.91667`

Time complexity: O(n) since using a single loop

Auxiliary Space: O(1) for constant space for variables

Previous
Next