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 <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
filter_none
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. |
chevron_right
filter_none
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 = 4print (round(sumOfSeries(n), 5)) # This code is contributed by # Manish Shaw(manishshaw1) |
chevron_right
filter_none
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. |
chevron_right
filter_none
PHP
<?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
filter_none
Output :
3.91667
Recommended Posts:
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ..... upto n terms
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Find sum of the series 1+22+333+4444+...... upto n terms
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Find n terms of Fibonacci type series with given first two terms
- Sum of the Tan(x) expansion upto N terms
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- Sum of the first N terms of the series 2,10, 30, 68,....
- Sum of the first N terms of the series 5,12, 23, 38....
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.



