# Find the sum of all the terms in the n-th row of the given series

Find the sum of all the terms in the **nth** row of the series given below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 .......................... ............................ (so on)

**Examples:**

Input : n = 2 Output : 18 terms in 2nd row and their sumsum= (3 + 4 + 5 + 6) = 18 Input : n = 4 Output : 132

**Naive Approach:** Using two loops. Outer loop executes for **i = 1 to n** times. Inner loop executes for **j = 1 to 2 * i** times. Counter variable **k** to keep track of the current term in the series. When **i = n**, the values of **k** are accumulated to the sum.

Time Complexity: O(k), where k is the total number of terms from the beginning till the end of the nth row.

**Efficient Approach:** The sum of all the terms in the **nth** row can be obtained by the formula:

Sum(n) = n * (2 * n^{2}+ 1)

The proof for the formula is given below:

**Prerequisite:**

- Sum of
**n**terms of an Arithmetic Progression series with**a**as the first term and**d**as the common difference is given as:**Sum = (n * [2*a + (n-1)*d]) / 2** - Sum of 1st
**n**natural numbers is given as:**Sum = (n * (n + 1)) / 2**

**Proof:**

Let the number of terms from the beginning till the end of thenthrow bep. Herep= 2 + 4 + 6 + .....nterms For the givenAPseries,a= 2,d= 2. Using the above formula for the sum ofnterms of the AP series, we get,p = n * (n + 1)Similarly, let the number of terms from the beginning till the end of the(n-1)throw beq. Hereq= 2 + 4 + 6 + .....n-1terms For the givenAPseries,a= 2,d= 2. Using the above formula for the sum ofn-1terms of the AP series, we get,q = n * (n - 1)Now, Sum of all the terms in the nth row = sum of 1stpnatural numbers - sum of 1stqnatural numbers =(p * (p + 1)) / 2 - (q * (q + 1)) / 2Substituting the values ofpandqand then solving the equation, we will get,Sum of all the terms in the nth row = n * (2 * n^{2}+ 1)

## C++

`// C++ implementation to find the sum of all the ` `// terms in the nth row of the given series ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// function to find the required sum ` `int` `sumOfTermsInNthRow(` `int` `n) ` `{ ` ` ` `// sum = n * (2 * n^2 + 1) ` ` ` `int` `sum = n * (2 * ` `pow` `(n, 2) + 1); ` ` ` `return` `sum; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` `cout << ` `"Sum of all the terms in nth row = "` ` ` `<< sumOfTermsInNthRow(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the sum of all the ` `// terms in the nth row of the given series ` ` ` `import` `static` `java.lang.Math.pow; ` ` ` `class` `Test { ` ` ` `// method to find the required sum ` ` ` `static` `int` `sumOfTermsInNthRow(` `int` `n) ` ` ` `{ ` ` ` `// sum = n * (2 * n^2 + 1) ` ` ` `int` `sum = (` `int` `)(n * (` `2` `* pow(n, ` `2` `) + ` `1` `)); ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `4` `; ` ` ` `System.out.println(` `"Sum of all the terms in nth row = "` ` ` `+ sumOfTermsInNthRow(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation to find ` `# the sum of all the terms in the ` `# nth row of the given series ` `from` `math ` `import` `pow` ` ` `# function to find the required sum ` `def` `sumOfTermsInNthRow(n): ` ` ` ` ` `# sum = n * (2 * n^2 + 1) ` ` ` `sum` `=` `n ` `*` `(` `2` `*` `pow` `(n, ` `2` `) ` `+` `1` `) ` ` ` `return` `sum` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `4` ` ` `print` `(` `"Sum of all the terms in nth row ="` `, ` ` ` `int` `(sumOfTermsInNthRow(n))) ` ` ` `# This code is contributed ` `# by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the sum of all the ` `// terms in the nth row of the given series ` `using` `System; ` ` ` `class` `Test { ` ` ` `// method to find the required sum ` ` ` `static` `int` `sumOfTermsInNthRow(` `int` `n) ` ` ` `{ ` ` ` `// sum = n * (2 * n^2 + 1) ` ` ` `int` `sum = (` `int` `)(n * (2 * Math.Pow(n, 2) + 1)); ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4; ` ` ` `Console.Write(` `"Sum of all the terms in nth row = "` ` ` `+ sumOfTermsInNthRow(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation to find ` `// the sum of all the terms in ` `// the nth row of the given series ` ` ` `// function to find the required sum ` `function` `sumOfTermsInNthRow(` `$n` `) ` `{ ` ` ` ` ` `// sum = n * (2 * n^2 + 1) ` ` ` `$sum` `= ` `$n` `* (2 * pow(` `$n` `, 2) + 1); ` ` ` `return` `$sum` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 4; ` ` ` `echo` `"Sum of all the terms in nth row = "` `, ` ` ` `sumOfTermsInNthRow(` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

Output:

Sum of all the terms in nth row = 132

This article is contributed by **Ayush Jauhari**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Find n terms of Fibonacci type series with given first two terms
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
- 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 ....
- Find sum of the series ?3 + ?12 +......... upto N terms
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k terms
- Find the sum of series 3, -6, 12, -24 . . . upto N terms
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ..... upto n terms
- Find sum of the series 1+22+333+4444+...... upto n terms
- Find the sum of the series 1+11+111+1111+..... upto n terms
- Program to find the sum of the series 23+ 45+ 75+..... upto N terms
- Sum of first n terms of a given series 3, 6, 11, .....
- 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....
- Sum of the first N terms of the series 5,12, 23, 38....