# Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++

C++Server Side ProgrammingProgramming

In this problem, we are given a number n which is the nth term of the series 1/(1*2) + 1/(2*3) +…+ 1/(n*(n+1)). Our task is to create a program to find the sum of the series.

## Let’s take an example to understand the problem,

Input

n = 3

Output

0.75

Explanation  sum = 1/(1*2) + 1/(2*3) + 1/(3*4) = ½ + ⅙+ 1/12 = (6+2+1)/12 = 9/12 = ¾ = 0.75

A simple solution to the problem is using the loop. And commuting value for each element of the series. Then add them to the sum value.

## Algorithm

Initialize sum = 0
Step 1: Iterate from i = 1 to n. And follow :
Step 1.1: Update sum, sum += 1/ ( i*(i+1) )
Step 2: Print sum.

## Example

Program to illustrate the working of our solution,

Live Demo

#include <iostream>
using namespace std;
double calcSeriesSum(int n) {
double sum = 0.0;
for (int i = 1; i <= n; i++)
sum += ((double)1/(i*(i+1)));
return sum;
}
int main() {
int n = 5;
cout<<"Sum of the series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... is "<<calcSeriesSum(n);
return 0;
}

## Output

Sum of the series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... is 0.833333

This solution is not much effective as it uses loops.

An effective approach to solve the problem is using the general formula for the sum of series.

The series is 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + …
n-th terms is 1/n(n+1).
an = 1/n(n+1)
an = ((n+1) - n) /n(n+1)
an = (n+1)/n(n+1) - n/ n(n+1)
an = 1/n - 1/(n+1)
sum of the series is
sum = 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + …
Changing each term as in above formula,
sum = 1/1 - ½ + ½ - ⅓ + ⅓ - ¼ + ¼ -⅕ + …. 1/n - 1/(n+1)
sum = 1 - 1/(n+1)
sum = (n+1 -1) / (n+1) = n/(n+1)

## Example

Program to illustrate the working of our solution,

Live Demo

#include <iostream>
using namespace std;
double calcSeriesSum(int n) {
return ((double)n/ (n+1));
}
int main() {
int n = 5;
cout<<"Sum of the series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... is "<<calcSeriesSum(n);
return 0;
}

## Output

Sum of the series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... is 0.833333
Published on 14-Aug-2020 17:31:23