Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . .
Given a positive integer n, The problem is to find the sum of the given series upto n terms:
1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . . . . + 1/(n*(n+1))
Examples :
Input : 3 Output : 0.75 ( 1/(1*2)+ 1/(2*3) + 1/(3*4) ) = (1/2 + 1/6 + 1/12) = 0.75 Input : 10 Output : 0.909 ( 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + 1/(5*6) + 1/(6*7) + 1/(7*8) + 1/(8*9) + 1/(9*10) + 1/(10*11) ) = (1/2 + 1/6 + 1/12 + 1/20 + 1/30 + 1/42 + 1/56 + 1/72 + 1/90 + 1/110) = 0.909
Naive Approach: Use a for loop to calculate each term iteratively and add to the final sum.
C++
// C++ program to find the sum of given series #include <bits/stdc++.h> using namespace std; // function to find the sum of given series double sumOfTheSeries(int n) { // Computing sum term by term double sum = 0.0; for (int i = 1; i <= n; i++) sum += 1.0 / (i * (i + 1)); return sum; } // driver program to test above function int main() { int n = 10; cout << sumOfTheSeries(n); return 0; } |
chevron_right
filter_none
Java
// Java program to find the sum of given series class demo { // function to find the sum of given series public static double sumOfTheSeries(int n) { // Computing sum term by term double sum = 0.0; for (int i = 1; i <= n; i++) sum += 1.0 / (i * (i + 1)); return sum; } // driver program to test above function public static void main(String args[]) { int n = 10; System.out.println(sumOfTheSeries(n)); } } |
chevron_right
filter_none
Python3
# Python3 code to find the sum of given series # Function to find the sum of given series def sumOfTheSeries( n ): # Computing sum term by term sum = 0 for i in range(1, n + 1): sum += 1.0 / (i * (i + 1)); return sum # Driver function if __name__ == '__main__': ans = sumOfTheSeries(10) # Rounding decimal value to 6th decimal place print (round(ans, 6)) # This code is contributed by 'saloni1297' |
chevron_right
filter_none
C#
// C# program to find the sum of given series using System; class demo { // Function to find the sum of given series public static double sumOfTheSeries(int n) { // Computing sum term by term double sum = 0.0; for (int i = 1; i <= n; i++) sum += 1.0 / (i * (i + 1)); return sum; } // Driver Code public static void Main() { int n = 10; Console.Write(sumOfTheSeries(n)); } } // This code is contributed by vt_m |
chevron_right
filter_none
PHP
<?php // PHP program to find the // sum of given series // function to find the // sum of given series function sumOfTheSeries( $n) { // Computing sum term by term $sum = 0.0; for ( $i = 1; $i <= $n; $i++) $sum += 1.0 / ($i * ($i + 1)); return $sum; } // Driver Code $n = 10; echo sumOfTheSeries($n); // This code is contributed by anuj_67 ?> |
chevron_right
filter_none
Output :
0.909091
Efficient Approach: Use the formula n/(n+1)
Validity of the formula:
Sum upto n terms = 1/(1*2) + 1/(2*3) + 1/(3*4) +
........ + 1/(n*(n+1))
where
1st term = 1/(1*2)
2nd term = 1/(2*3)
3rd term = 1/(3*4)
.
.
.
.
n-th term = 1/(n*(n+1))
i.e. the k-th term is of the form 1/(k*(k+1))
which can further be written as k-th term =
1/k - 1/(k+1)
So sum upto n terms can be calculated as:
(1/1 - 1/1+1) + (1/2 - 1/2+1) + (1/3 - 1/3+1)
+ ......... + (1/n-1 - /1n) + (1/n - 1/n+1)
= (1 - 1/2) + (1/2 - 1/3) + (1/3 - 1/4) + .........
+ (1/n-1 - 1/n) + (1/n - 1/n+1)
= 1 - 1/n+1
= ((n+1) - 1)/n+1
= n/n+1
Hence sum upto n terms = n/n+1
C++
// C++ program to find sum of given series #include <bits/stdc++.h> using namespace std; // function to find sum of given series double sumOfTheSeries(int n) { // type-casting n/n+1 from int to double return (double)n / (n + 1); } // driver program to test above function int main() { int n = 10; cout << sumOfTheSeries(n); return 0; } |
chevron_right
filter_none
Java
// Java program to find sum of given series class demo { // function to find sum of given series public static double sumOfTheSeries(int n) { // type -casting n/n+1 from int to double return(double)n / (n + 1); } // driver program to test above function public static void main(String args[]) { int n = 10; System.out.println(sumOfTheSeries(n)); } } |
chevron_right
filter_none
Python3
# Python3 code to find sum of given series # Function to find sum of given series def sumOfTheSeries(n): # Type-casting n/n+1 from int to float return (float(n) / (n + 1)) # Driver function if __name__ == '__main__': n = 10 ans = sumOfTheSeries(n) # Rounding decimal value print (round(ans, 6)) # This code is contributed by 'saloni1297' |
chevron_right
filter_none
C#
// C# program to find sum of given series using System; class demo { // Function to find sum of given series public static double sumOfTheSeries(int n) { // type -casting n/n+1 from int to double return(double)n / (n + 1); } // Driver Code public static void Main() { int n = 10; Console.Write(sumOfTheSeries(n)); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP program to find // sum of given series // function to find sum // of given series function sumOfTheSeries($n) { // type-casting n/n+1 // from int to double return $n / ($n + 1); } // Driver Code $n = 10; echo sumOfTheSeries($n); // This code is contributed // by SanjuTomar ?> |
chevron_right
filter_none
Output :
0.909091
Recommended Posts:
- Sum of series 1*1! + 2*2! + ……..+ n*n!
- Sum of series 1*1*2! + 2*2*3! + ……..+ n*n*(n+1)!
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1))
- Sum of series (n/1) + (n/2) + (n/3) + (n/4) +.......+ (n/n)
- Sum of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + …… + (2+4+6+8+….+2n)
- Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ....... (K-1)n
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ...... + (1+2+3+4+...+n)
- Sum of the series 3, 20, 63, 144, ......
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2
- Sum of Series (n^2-1^2) + 2(n^2-2^2) +....n(n^2-n^2)
- Sum of the series 1.2.3 + 2.3.4 + ... + n(n+1)(n+2)
- Sum of the series 2^0 + 2^1 + 2^2 +.....+ 2^n
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- 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.

