Given the value of the n. You have to find the sum of the series where the n^{th} term of the sequence is given by:

T_{n} = n^{2} – ( n – 1 )^{2}

**Examples :**

Input : 3 Output : 9 Explanation: So here the tern of the sequence upto n = 3 are: 1, 3, 5 And hence the required sum is = 1 + 3 + 5 = 9 Input : 6 Output : 36

**Simple Approach**

Just use a loop and calculate the sum of each term and print the sum.

## C++

`// CPP program to find summation of series ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `summingSeries(` `long` `n) ` `{ ` ` ` `// use of loop to calculate ` ` ` `// sum of each term ` ` ` `int` `S = 0; ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `S += i * i - (i - 1) * (i - 1); ` ` ` ` ` `return` `S; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 100; ` ` ` `cout << ` `"The sum of n term is: "` ` ` `<< summingSeries(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA program to find summation of series ` `import` `java.io.*; ` `import` `java.math.*; ` `import` `java.text.*; ` `import` `java.util.*; ` `import` `java.util.regex.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// function to calulate sum of series ` ` ` `static` `int` `summingSeries(` `long` `n) ` ` ` `{ ` ` ` `// use of loop to calculate ` ` ` `// sum of each term ` ` ` `int` `S = ` `0` `; ` ` ` `for` `(i = ` `1` `; i <= n; i++) ` ` ` `S += i * i - (i - ` `1` `) * (i - ` `1` `); ` ` ` ` ` `return` `S; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `100` `; ` ` ` `System.out.println(` `"The sum of n term is: "` `+ ` ` ` `summingSeries(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find summation ` `# of series ` ` ` `def` `summingSeries(n): ` ` ` ` ` `# use of loop to calculate ` ` ` `# sum of each term ` ` ` `S ` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `, n` `+` `1` `): ` ` ` `S ` `+` `=` `i ` `*` `i ` `-` `(i ` `-` `1` `) ` `*` `(i ` `-` `1` `) ` ` ` ` ` `return` `S ` ` ` `# Driver Code ` `n ` `=` `100` `print` `(` `"The sum of n term is: "` `, ` ` ` `summingSeries(n), sep ` `=` `"") ` `# This code is contributed by Smitha. ` |

*chevron_right*

*filter_none*

## C#

` ` `// C# program to illustrate... ` `// Summation of series ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// function to calculate sum of series ` ` ` `static` `int` `summingSeries(` `long` `n) ` ` ` `{ ` ` ` ` ` `// Using the pow function calculate ` ` ` `// the sum of the series ` ` ` `return` `(` `int` `)Math.Pow(n, 2); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `n = 100; ` ` ` `Console.Write(` `"The sum of n term is: "` `+ ` ` ` `summingSeries(n)); ` ` ` `} ` `} ` ` ` `// This code contribute by Parashar... ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// summation of series ` ` ` `function` `summingSeries( ` `$n` `) ` `{ ` ` ` ` ` `// use of loop to calculate ` ` ` `// sum of each term ` ` ` `$S` `= 0; ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$S` `+= ` `$i` `* ` `$i` `- (` `$i` `- 1) * ` ` ` `(` `$i` `- 1); ` ` ` ` ` `return` `$S` `; ` `} ` ` ` `// Driver Code ` `$n` `= 100; ` `echo` `"The sum of n term is: "` `, ` `summingSeries(` `$n` `) ; ` ` ` `// This code contribute by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

The sum of n term is: 10000

**Time complexity –** O(N)

**Space complexity –** O(1)

**Efficient Approach**

Use of mathematical approach can solve this problem in more efficient way.

T

_{n}= n^{2}– (n-1)^{2}Sum of the series is given by (S) = SUM( T

_{n})LET US TAKE A EXAMPLE IF

N = 4

It means there should be 4 terms in the series so1

^{st}term = 1^{2}– ( 1 – 1 )^{2}

2^{nd}term = 2^{2}– ( 2 – 1 )^{2}

3^{th}term = 3^{2}– ( 3 – 1 )^{2}

4^{th}term = 4^{2}– ( 3 – 1 )^{2}

SO SUM IS GIVEN BY= (1 – 0) + (4 – 1) + (9 – 4) + (16 – 9)

= 16FROM THIS WE HAVE NOTICE THAT 1, 4, 9 GET CANCELLED FROM THE SERIES

ONLY 16 IS LEFT WHICH IS EQUAL TO THE SQUARE OF NSo from the above series we notice that each term gets canceled from the next term, only the last term is left which is equal to N

^{2}.

## C++

`// CPP program to illustrate... ` `// Summation of series ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `summingSeries(` `long` `n) ` `{ ` ` ` `// Sum of n terms is n^2 ` ` ` `return` `pow` `(n, 2); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 100; ` ` ` `cout << ` `"The sum of n term is: "` ` ` `<< summingSeries(n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA program to illustrate... ` `// Summation of series ` ` ` `import` `java.io.*; ` `import` `java.math.*; ` `import` `java.text.*; ` `import` `java.util.*; ` `import` `java.util.regex.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// function to calculate sum of series ` ` ` `static` `int` `summingSeries(` `long` `n) ` ` ` `{ ` ` ` ` ` `// Using the pow function calculate ` ` ` `// the sum of the series ` ` ` `return` `(` `int` `)Math.pow(n, ` `2` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `100` `; ` ` ` `System.out.println(` `"The sum of n term is: "` `+ ` ` ` `summingSeries(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to illustrate... ` `# Summation of series ` `import` `math ` ` ` `def` `summingSeries(n): ` ` ` ` ` `# Sum of n terms is n^2 ` ` ` `return` `math.` `pow` `(n, ` `2` `) ` ` ` `# Driver Code ` `n ` `=` `100` `print` `(` `"The sum of n term is: "` `, ` ` ` `summingSeries(n)) ` `# This code is contributed by mits. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to illustrate... ` `// Summation of series ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// function to calculate sum of series ` ` ` `static` `int` `summingSeries(` `long` `n) ` ` ` `{ ` ` ` `// Using the pow function calculate ` ` ` `// the sum of the series ` ` ` `return` `(` `int` `)Math.Pow(n, 2); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 100; ` ` ` `Console.Write(` `"The sum of n term is: "` `+ ` ` ` `summingSeries(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to illustrate... ` `// Summation of series ` ` ` `function` `summingSeries(` `$n` `) ` `{ ` ` ` `// Sum of n terms is n^2 ` ` ` `return` `pow(` `$n` `, 2); ` `} ` ` ` `// Driver Code ` `$n` `= 100; ` `echo` `"The sum of n term is: "` `, ` `summingSeries(` `$n` `); ` ` ` `// This code contribute by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

The sum of n term is: 10000

Time complexity – O(1)

Space complexity – O(1)

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:

- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Program to print tetrahedral numbers upto Nth term
- Program to print pentatope numbers upto Nth term
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- Program for sum of cosh(x) series upto Nth term
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find Nth term of series 9, 23, 45, 75, 113...
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
- Program to find Nth term of the series 3, 6, 18, 24, ...

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.