# Sum of first n terms of a given series 3, 6, 11, …..

Given a series and a number n, the task is to find the sum of its first n terms. Below is the series:

3, 6, 11, 20, ….

**Examples:**

Input: N = 2 Output: 9 The sum of first 2 terms of Series is 3 + 6 = 9 Input: N = 3 Output: 20 The sum of first 3 terms of Series is 3 + 6 + 11 = 20

**Approach:** This problem can easily solved by observing that the nth term of the series :

Sn = 3 + 6 + 11 + 20 … + upto nth term

Sn = (1 + 2^1) + (2 + 2^2) + (3 + 2^3)+ (4 + 2^4) …… + upto nth term

Sn = (1 + 2 + 3 + 4 …. + upto nth term) + ( 2^1 + 2^2 + 2^3 …… + unto nth term )

We observe that Sn is a summation of two series: **AP and GP**

As we know the sum of first n terms of AP is given by

And also the Sum of first n terms of G.P is given by

Hence the total sum is given by sum of both AP and GP.

Below is the implementation of above approach.

## C++

`// C++ program to find sum of first n terms ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate the sum ` `int` `calculateSum(` `int` `n) ` `{ ` ` ` `// starting number ` ` ` `int` `a1 = 1, a2 = 2; ` ` ` ` ` `// Common Ratio ` ` ` `int` `r = 2; ` ` ` ` ` `// Common difference ` ` ` `int` `d = 1; ` ` ` ` ` `return` `(n) * (2 * a1 + (n - 1) * d) / 2 + a2 ` ` ` `* (` `pow` `(r, n) - 1) / (r - 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `// N th term to be find ` ` ` `int` `n = 5; ` ` ` ` ` `// find the Sn ` ` ` `cout << ` `"Sum = "` `<< calculateSum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of first n terms ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to calculate the sum ` `static` `int` `calculateSum(` `int` `n) ` `{ ` ` ` `// starting number ` ` ` `int` `a1 = ` `1` `, a2 = ` `2` `; ` ` ` ` ` `// Common Ratio ` ` ` `int` `r = ` `2` `; ` ` ` ` ` `// Common difference ` ` ` `int` `d = ` `1` `; ` ` ` ` ` `return` `(n) * (` `2` `* a1 + (n - ` `1` `) * d) / ` `2` `+ a2 ` ` ` `* (` `int` `)(Math.pow(r, n) - ` `1` `) / (r - ` `1` `); ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `// N th term to be find ` ` ` `int` `n = ` `5` `; ` ` ` ` ` `// find the Sn ` ` ` `System.out.print( ` `"Sum = "` `+ calculateSum(n)); ` ` ` `} ` `} ` `// This code is contributed by inder_verma. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find ` `# sum of first n terms ` `def` `calculateSum(n): ` ` ` `# First term of AP ` ` ` `a1 ` `=` `1` `; ` ` ` ` ` `# First term of GP ` ` ` `a2 ` `=` `2` `; ` ` ` ` ` `# common ratio of GP ` ` ` `r ` `=` `2` `; ` ` ` ` ` `# common difference Of AP ` ` ` `d ` `=` `1` `; ` ` ` `return` `((n) ` `*` `(` `2` `*` `a1 ` `+` `(n ` `-` `1` `) ` `*` `d) ` `/` ` ` `2` `+` `a2 ` `*` `(` `pow` `(r, n) ` `-` `1` `) ` `/` ` ` `(r ` `-` `1` `)); ` ` ` `# Driver Code ` ` ` `# no. of the terms ` `# for the sum ` `n ` `=` `5` `; ` ` ` `# Find the Sn ` `print` `(` `"Sum ="` `, ` `int` `(calculateSum(n))) ` ` ` `# This code is contributed ` `# by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum ` `// of first n terms ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to calculate the sum ` `static` `int` `calculateSum(` `int` `n) ` `{ ` ` ` `// starting number ` ` ` `int` `a1 = 1, a2 = 2; ` ` ` ` ` `// Common Ratio ` ` ` `int` `r = 2; ` ` ` ` ` `// Common difference ` ` ` `int` `d = 1; ` ` ` ` ` `return` `(n) * (2 * a1 + (n - 1) * d) / 2 + a2 * ` ` ` `(` `int` `)(Math.Pow(r, n) - 1) / (r - 1); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `// N th term to be find ` ` ` `int` `n = 5; ` ` ` ` ` `// find the Sn ` ` ` `Console.WriteLine(` `"Sum = "` `+ calculateSum(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by inder_verma ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum of first n terms ` ` ` `// Function to calculate the sum ` `function` `calculateSum(` `$n` `) ` `{ ` ` ` `// starting number ` ` ` `$a1` `= 1; ` ` ` `$a2` `= 2; ` ` ` ` ` `// Common Ratio ` ` ` `$r` `= 2; ` ` ` ` ` `// Common difference ` ` ` `$d` `= 1; ` ` ` ` ` `return` `(` `$n` `) * (2 * ` `$a1` `+ (` `$n` `- 1) * ` `$d` `) / 2 + ` ` ` `$a2` `* (pow(` `$r` `, ` `$n` `) - 1) / (` `$r` `- 1); ` `} ` ` ` `// Driver code ` ` ` `// Nth term to be find ` `$n` `= 5; ` ` ` `// find the Sn ` `echo` `"Sum = "` `, calculateSum(` `$n` `); ` ` ` `// This code is contributed ` `// by Shashank_Sharma ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Sum = 77

## Recommended Posts:

- Find n terms of Fibonacci type series with given first two terms
- Sum of the series 5+55+555+.. up to n terms
- Sum of the first N terms of the series 5,12, 23, 38....
- Sum of the first N terms of the series 2,10, 30, 68,....
- Sum of the first N terms of the series 2, 6, 12, 20, 30....
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Find the sum of n terms of the series 1,8,27,64 ....
- 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
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Find the sum of all the terms in the n-th row of the given series
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
- Sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms

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.