Given a series:

S

_{n}= 1*3 + 3*5 + 5*7 + …

It is required to find the sum of first n terms of this series represented by S_{n}, where n is given taken input.

**Examples**:

Input: n = 2Output: S<sub>n</sub> = 18Explanation: The sum of first 2 terms of Series is 1*3 + 3*5 = 3 + 15 = 28Input: n = 4Output: S<sub>n</sub> = 116Explanation: The sum of first 4 terms of Series is 1*3 + 3*5 + 5*7 + 7*9 = 3 + 15 + 35 + 63 = 116

Let, the n-th term be denoted by t_{n}.

This problem can easily be solved by observing that the nth term can be founded by following method:

t

_{n}= (n-th term of (1, 3, 5, … ) )*(nth term of (3, 5, 7, ….))

Now, n-th term of series 1, 3, 5 is given by **2*n-1**

and, the n-th term of series 3, 5, 7 is given by **2*n+1**

Putting these two values in t_{n}:

t

_{n}= (2*n-1)*(2*n+1) = 4*n*n-1

Now, the sum of first n terms will be given by :

S

_{n}= ∑(4*n*n – 1)

=∑4*{n*n}-∑(1)

Now, it is known that the sum of first n terms of series n*n (1, 4, 9, …) is given by: n*(n+1)*(2*n+1)/6

And sum of n number of 1’s is n itself.

Now, putting values in S_{n}:

S

_{n}= 4*n*(n+1)*(2*n+1)/6 – n

= n*(4*n*n + 6*n – 1)/3

Now, S_{n} value can be easily found by putting the desired value of n.

Below is the implementation of the above approach:

## C++

`// C++ program to find sum of first n terms ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `calculateSum(` `int` `n) ` `{ ` ` ` `// Sn = n*(4*n*n + 6*n - 1)/3 ` ` ` `return` `(n * (4 * n * n + 6 * n - 1) / 3); ` `} ` ` ` `int` `main() ` `{ ` ` ` `// number of terms to be included in the sum ` ` ` `int` `n = 4; ` ` ` ` ` `// find the Sn ` ` ` `cout << ` `"Sum = "` `<< calculateSum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum ` `// of first n terms ` `class` `GFG ` `{ ` ` ` `static` `int` `calculateSum(` `int` `n) ` ` ` `{ ` ` ` `// Sn = n*(4*n*n + 6*n - 1)/3 ` ` ` `return` `(n * (` `4` `* n * n + ` ` ` `6` `* n - ` `1` `) / ` `3` `); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `// number of terms to be ` ` ` `// included in the sum ` ` ` `int` `n = ` `4` `; ` ` ` ` ` `// find the Sn ` ` ` `System.out.println(` `"Sum = "` `+ ` ` ` `calculateSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Bilal ` |

*chevron_right*

*filter_none*

## Python

`# Python program to find sum ` `# of first n terms ` `def` `calculateSum(n): ` ` ` ` ` `# Sn = n*(4*n*n + 6*n - 1)/3 ` ` ` `return` `(n ` `*` `(` `4` `*` `n ` `*` `n ` `+` ` ` `6` `*` `n ` `-` `1` `) ` `/` `3` `); ` ` ` `# Driver Code ` ` ` `# number of terms to be ` `# included in the sum ` `n ` `=` `4` ` ` `# find the Sn ` `print` `(` `"Sum ="` `,calculateSum(n)) ` ` ` `# This code is contributed by Bilal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum ` `// of first n terms ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `calculateSum(` `int` `n) ` `{ ` ` ` `// Sn = n*(4*n*n + 6*n - 1)/3 ` ` ` `return` `(n * (4 * n * n + ` ` ` `6 * n - 1) / 3); ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `// number of terms to be ` ` ` `// included in the sum ` ` ` `int` `n = 4; ` ` ` ` ` `// find the Sn ` ` ` `Console.WriteLine(` `"Sum = "` `+ ` ` ` `calculateSum(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by mahadev ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum ` `// of first n terms ` ` ` `function` `calculateSum(` `$n` `) ` `{ ` ` ` `// Sn = n*(4*n*n + 6*n - 1)/3 ` ` ` `return` `(` `$n` `* (4 * ` `$n` `* ` `$n` `+ ` ` ` `6 * ` `$n` `- 1) / 3); ` `} ` ` ` `// number of terms to be ` `// included in the sum ` `$n` `= 4; ` ` ` `// find the Sn ` `echo` `"Sum = "` `. calculateSum(` `$n` `); ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Sum = 116

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find the sum of the series 1 + x + x^2+ x^3+ .. + x^n
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find nth term of the series 1 4 15 24 45 60 92
- Program to find Nth term in the given Series
- Program to find Nth term of series 7, 21, 49, 91, 147, 217, ......
- Program to find Nth term of the series 3, 6, 18, 24, ...
- Program to find Nth term of the series 2, 4, 3, 4, 15...
- Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,...
- Program to find the N-th term of series 3, 5, 33, 35, 53…. | Set-2

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.