Program to print the sum of the given nth term

09 Aug, 2021
Given the value of the n. You have to find the sum of the series where the nth term of the sequence is given by:Tn = n2 – ( n – 1 )2Examples : 
Input : 3
Output : 9

Explanation: So here the term 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++
Java
Python3
C#
PHP
Javascript
// 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 Codeint main(){    int n = 100;    cout << "The sum of n term is: "        << summingSeries(n) << endl;    return 0;}
// JAVA program to find summation of seriesimport 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)    {        // 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));    }}
# 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 Coden = 100print("The sum of n term is: ",           summingSeries(n), sep = "")# This code is contributed by Smitha.
// C# program to illustrate...// Summation of seriesusing 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...
<?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.?>
<script>// Javascript program to find summation of series function summingSeries(n){    // use of loop to calculate    // sum of each term    let S = 0;    for (let i = 1; i <= n; i++)        S += i * i - (i - 1) * (i - 1);         return S;} // Driver Codelet n = 100;document.write("The sum of n term is: " + summingSeries(n)); // This code is contributed by rishavmahato348.</script>
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. 
Tn = n2 – (n-1)2Sum of the series is given by (S) = SUM( Tn )LET US TAKE A EXAMPLE IF N = 4 It means there should be 4 terms in the series so1st term = 12 – ( 1 – 1 )2 2nd term = 22 – ( 2 – 1 )2 3th term = 32 – ( 3 – 1 )2 4th term = 42 – ( 3 – 1 )2SO 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 N2. 
C++
Java
Python3
C#
PHP
Javascript
// 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 Codeint main(){    int n = 100;    cout << "The sum of n term is: "         << summingSeries(n) << endl;    return 0;}
// 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));    }}
# Python3 program to illustrate...# Summation of seriesimport math def summingSeries(n):     # Sum of n terms is  n^2    return math.pow(n, 2) # Driver Coden = 100print ("The sum of n term is: ",        summingSeries(n))# This code is contributed by mits.
// C# program to illustrate...// Summation of seriesusing 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.
<?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.?>
<script>// Javascript program to illustrate...// Summation of series function summingSeries(n){    // Sum of n terms is n^2    return Math.pow(n, 2);} // Driver Codelet n = 100;document.write("The sum of n term is: "    + summingSeries(n) + "<br>");         // This code is contributed by subham348.</script>
Output: 
The sum of n term is: 10000
Time complexity – O(1) Space complexity – O(1)  
parashar
nitin mittal
Smitha Dinesh Semwal
vt_m
Mithun Kumar
Akanksha_Rai
shubham prakash 1
rishavmahato348
subham348
simmytarika5
surindertarika1234
series-sum
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operators in C / C++
Prime Numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Modulo 10^9+7 (1000000007)
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number
Program to find sum of elements in a given array