Open In App

Program to print the sum of the given nth term

Last Updated : 29 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 )2
Examples : 

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++




// 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;
}


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 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




# 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.


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...


Javascript




<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 Code
let n = 100;
document.write("The sum of n term is: " + summingSeries(n));
 
// This code is contributed by rishavmahato348.
</script>


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.
?>


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)2
Sum 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 so
1st term = 12 – ( 1 – 1 )2 
2nd term = 22 – ( 2 – 1 )2 
3th term = 32 – ( 3 – 1 )2 
4th term = 42 – ( 3 – 1 )2
SO SUM IS GIVEN BY = (1 – 0) + (4 – 1) + (9 – 4) + (16 – 9) 
= 16
FROM THIS WE HAVE NOTICE THAT 1, 4, 9 GET CANCELLED FROM THE SERIES 
ONLY 16 IS LEFT WHICH IS EQUAL TO THE SQUARE OF N
So 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++




// 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;
}


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));
    }
}


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.


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.


Javascript




<script>
// Javascript program to illustrate...
// Summation of series
 
function summingSeries(n)
{
    // Sum of n terms is n^2
    return Math.pow(n, 2);
}
 
// Driver Code
let n = 100;
document.write("The sum of n term is: "
    + summingSeries(n) + "<br>");
     
    // This code is contributed by subham348.
</script>


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.
?>


Output: 

The sum of n term is: 10000

Time complexity – O(1) 
Space complexity – O(1) 
 

Approach#3: Using for loop

Take the value of ‘n’ from the user. Initialize the sum variable to 0. Use a for loop to iterate over the range of 1 to 2*n with a step size of 2. In each iteration, add the current value to the sum variable. After the loop, print the sum of the first n terms of the sequence.

Algorithm

1. Start
2. Take the value of ‘n’ from the user and store it in a variable.
3. Initialize the sum variable to 0.
4. Use a for loop to iterate over the range of 1 to 2*n with a step size of 2.
a. Add the current value to the sum variable.
5. After the loop, print the sum of the first n terms of the sequence.
End

C++




#include <iostream>
using namespace std;
 
int main() {
    // Given value of n
    const int n = 100;
 
    // Initialize the sum
    int sum = 0;
 
    // Loop through odd numbers from 1 to 2*n with step 2
    for (int i = 1; i < 2 * n; i += 2) {
        // Add the current odd number to the sum
        sum += i;
    }
 
    // Print the sum of the first n terms of the sequence
    cout << "Sum of first " << n << " terms of the sequence is: " << sum <<endl;
 
    return 0;
}


Java




public class SumOfOddNumbers {
    public static void main(String[] args)
    {
        // Given value of n
        final int n = 100;
 
        // Initialize the sum
        int sum = 0;
 
        // Loop through odd numbers from 1 to 2*n with step
        // 2
        for (int i = 1; i < 2 * n; i += 2) {
            // Add the current odd number to the sum
            sum += i;
        }
 
        // Print the sum of the first n terms of the
        // sequence
        System.out.println("Sum of first " + n
                           + " terms of the sequence is: "
                           + sum);
    }
}


Python3




n = 100
sum = 0
for i in range(1, 2*n, 2):
    sum += i
print("Sum of first {} terms of the sequence is: {}".format(n, sum))


C#




using System;
 
class Program {
    static void Main()
    {
        // Given value of n
        const int n = 100;
 
        // Initialize the sum
        int sum = 0;
 
        // Loop through odd numbers from 1 to 2*n with step
        // 2
        for (int i = 1; i < 2 * n; i += 2) {
            // Add the current odd number to the sum
            sum += i;
        }
 
        // Print the sum of the first n terms of the
        // sequence
        Console.WriteLine("Sum of first " + n
                          + " terms of the sequence is: "
                          + sum);
    }
}


Javascript




// Given value of n
const n = 100;
 
// Initialize the sum
let sum = 0;
 
// Loop through odd numbers from 1 to 2*n with step 2
for (let i = 1; i < 2 * n; i += 2) {
    // Add the current odd number to the sum
    sum += i;
}
 
// Print the sum of the first n terms of the sequence
console.log(`Sum of first ${n} terms of the sequence is: ${sum}`);


Output

Sum of first 100 terms of the sequence is: 10000








Time Complexity: O(n) The for loop iterates n times.
Space Complexity: O(1) Only a constant amount of extra space is required to store the sum and the loop variable ‘i’.



Similar Reads

Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
Given two integers N and D, the task is to find the value of F(N) where the value of F(1) is D, where F(K) is given as: [Tex]F(K+1) = F(K) * (Max_{Digit}(F(K)) - Min_{Digit}(F(K)))[/Tex] Examples: Input: N = 3, D = 487 Output: 15584 Explanation: As F(1) = 487, F(2) = 487 * (maxDigit(487) - minDigit(487)) = 487 * 4 = 1948 F(3) = 1948 * (maxDigit(194
6 min read
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
Given two numbers N and K, where K represents the starting term of the sequence. The task is to find the Nth term of a sequence formed by sum of current term with product of largest and smallest digit of current term, i.e., AN+1 = AN + max(digits of AN) * min(digits of AN) Examples: Input: K = 1, N = 5 Output: 50 Explanation: A1 = 1 A2 = A1 + minDi
6 min read
Nth term of given recurrence relation having each term equal to the product of previous K terms
Given two positive integers N and K and an array F[] consisting of K positive integers. The Nth term of the recurrence relation is given by: FN = FN - 1 * FN - 2 * FN - 3 *.......* FN - K The task is to find the Nth term of the given recurrence relation. As the Nth term can be very large, print the Nth term modulo 109 + 7. Examples: Input: N = 5, K
16 min read
First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
Given an integer N and an integer FN which denotes the Nth term of the linear equation F(N) = (2 * F(N - 1)) % M, where M is 109 + 7, the task is to find the value of F(1). Examples: Input : N = 2, FN = 6 Output:3 Explanation: If F(1) = 3, F(2) = (2 * F(1)) % M = (2 * 3) % M = 6. For F(1) = 3 the given linear equation satisfies the value of F(2). T
8 min read
Find Nth term of the series where each term differs by 6 and 2 alternately
Given a number N, the task is to find the Nth term of the series where each term differs by 6 and 2 alternately.Examples: Input: N = 6 Output: 24 Explanation: The Nth term is 0 + 6 + 2 + 6 + 2 + 6 + 2 = 24Input: N = 3 Output: 14 Explanation: The Nth term is 0 + 6 + 2 + 6 = 14 Naive Approach: The idea is to iterate from 1 with an increment of 6 and
7 min read
Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
Given three integers X, Y and N, the task is to find the Nth term of the series f[i] = f[i - 1] - f[i - 2], i &gt; 1 where f[0] = X and f[1] = Y.Examples: Input: X = 2, Y = 3, N = 3 Output: -2 The series will be 2 3 1 -2 -3 -1 2 and f[3] = -2Input: X = 3, Y = 7, N = 8 Output: 4 Approach: An important observation here is that there will be atmost 6
10 min read
Program to print tetrahedral numbers upto Nth term
Prerequisites: Triangular numbersTetrahedral numbers Given a value n, and the task is to print tetrahedral number series up to nth term.Examples: Input: 5 Output: 1 4 10 20 35 Input: 10 Output: 1 4 10 20 35 56 84 120 165 220 Method 1: Using Triangular Number series: This problem can be easily solved with the fact that Nth Tetrahedral Number is equa
9 min read
Program to print pentatope numbers upto Nth term
Prerequisites: Tetrahedral numbersPentatope Numbers Given a value n, and the task is to print pentatope numbers series up to nth term.Examples: Input: 5 Output: 1 5 15 35 70 Input: 10 Output: 1 5 15 35 70 126 210 330 495 715 Method 1: Using Tetrahedral Number Series: This problem can be easily solved with the fact that Nth Pentatope Number is equal
10 min read
Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
This is a mathematical series program where a user must enter the number of terms up to which the sum of the series is to be found. Following this, we also need the value of x, which forms the base of the series. Examples: Input : x = 9, n = 10 Output : -5.1463 Input : x = 5, n = 15 Output : 0.2837 Simple approach : We use two nested loops to compu
10 min read
Program for sum of cosh(x) series upto Nth term
Given two numbers x and N, the task is to find the value of cosh(x) from the series upto N terms.The expansion of cosh(x) is given below: cosh(x) = 1 + x2/2! + x4/4! + ............ Examples: Input: x = 1, N = 5 Output: 1.54308035714 Input: x = 1, N = 10 Output: 1.54308063497 Approach: The above series can be easily implemented using a factorial fun
4 min read
Article Tags :
Practice Tags :