Sum of the series 1.2.3 + 2.3.+ … + n(n+1)(n+2) in C


Find the sum up to n terms of the series: 1.2.3 + 2.3.4 + … + n(n+1)(n+2). In this 1.2.3 represent the first term and 2.3.4 represent the second term.

Let’s see an example to understand the concept better,

Input: n = 5
Output: 420

Explanation

1.2.3 + 2.3.4 + 3.4.5 + 4.5.6 + 5.6.7 = 6 + 24 + 60 + 120 + 210 = 420

nth term = n(n+1)(n+2); where n = 1,2,3,…

= n(n^2+3n+2)=n^3 +3n^2 +2n

Now,note

Sum =n(n+1)/2 ; if nth term =n

=n(n+1)(2n+1)/6 ; if nth term =n^2

=n^2(n+1)^2/4 ; if nth term =n^3

Hence the required sum =

n^2(n+1)^2 /4 + 3 ×n(n+1)(2n+1)/6 +2 × n(n+1)/2

=n^2 (n+1)^2 /4 +n(n+1)(2n+1)/2 + n(n+1)

=n(n+1) { n(n+1)/4 + (2n+1)/2 +1 }

=n(n+1) { (n^2 +n +4n+2 +4)/4}

=1/4 n(n+1){ n^2+5n+6}

=1/4 n(n+1)(n+2)(n+3)

There are two methods to solve this problem,

One by using the mathematical formula and other by a loop.

In mathematical formula method, the sum of series formula for this series is given.

Algorithm

Input: n the number of elements.

Step 1 : calc the sum,
   sum = 1/4{n(n+1)(n+2)(n+3)}
Step 2 : Print sum, using standard print method.

Example

 Live Demo

#include <stdio.h>
#include<math.h>
int main() {
   float n = 6;
   float area = n*(n+1)*(n+2)*(n+3)/4;
   printf("The sum is : %f",area);
   return 0;
}

Output

The sum is : 756

Example

 Live Demo

#include <stdio.h>
#include<math.h>
int main() {
   float n = 6;
   int res = 0;
   for (int i = 1; i <= n; i++)
      res += (i) * (i + 1) * (i + 2);
   printf("The sum is : %d",res);
   return 0;
}

Output

The sum is : 756
raja
Published on 07-Oct-2019 07:22:15
Advertisements