Program to print the sum of the given nth term
Last Updated :
29 Oct, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int summingSeries( long n)
{
int S = 0;
for ( int i = 1; i <= n; i++)
S += i * i - (i - 1) * (i - 1);
return S;
}
int main()
{
int n = 100;
cout << "The sum of n term is: "
<< summingSeries(n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
class GFG
{
static int summingSeries( long n)
{
int S = 0 ;
for (i = 1 ; i <= n; i++)
S += i * i - (i - 1 ) * (i - 1 );
return S;
}
public static void main(String[] args)
{
int n = 100 ;
System.out.println( "The sum of n term is: " +
summingSeries(n));
}
}
|
Python3
def summingSeries(n):
S = 0
for i in range ( 1 , n + 1 ):
S + = i * i - (i - 1 ) * (i - 1 )
return S
n = 100
print ( "The sum of n term is: " ,
summingSeries(n), sep = "")
|
C#
using System;
class GFG
{
static int summingSeries( long n)
{
return ( int )Math.Pow(n, 2);
}
public static void Main(String[] args)
{
int n = 100;
Console.Write( "The sum of n term is: " +
summingSeries(n));
}
}
|
Javascript
<script>
function summingSeries(n)
{
let S = 0;
for (let i = 1; i <= n; i++)
S += i * i - (i - 1) * (i - 1);
return S;
}
let n = 100;
document.write( "The sum of n term is: " + summingSeries(n));
</script>
|
PHP
<?php
function summingSeries( $n )
{
$S = 0;
for ( $i = 1; $i <= $n ; $i ++)
$S += $i * $i - ( $i - 1) *
( $i - 1);
return $S ;
}
$n = 100;
echo "The sum of n term is: " ,
summingSeries( $n ) ;
?>
|
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++
#include <bits/stdc++.h>
using namespace std;
int summingSeries( long n)
{
return pow (n, 2);
}
int main()
{
int n = 100;
cout << "The sum of n term is: "
<< summingSeries(n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
class GFG
{
static int summingSeries( long n)
{
return ( int )Math.pow(n, 2 );
}
public static void main(String[] args)
{
int n = 100 ;
System.out.println( "The sum of n term is: " +
summingSeries(n));
}
}
|
Python3
import math
def summingSeries(n):
return math. pow (n, 2 )
n = 100
print ( "The sum of n term is: " ,
summingSeries(n))
|
C#
using System;
class GFG
{
static int summingSeries( long n)
{
return ( int )Math.Pow(n, 2);
}
public static void Main()
{
int n = 100;
Console.Write( "The sum of n term is: " +
summingSeries(n));
}
}
|
Javascript
<script>
function summingSeries(n)
{
return Math.pow(n, 2);
}
let n = 100;
document.write( "The sum of n term is: "
+ summingSeries(n) + "<br>" );
</script>
|
PHP
<?php
function summingSeries( $n )
{
return pow( $n , 2);
}
$n = 100;
echo "The sum of n term is: " ,
summingSeries( $n );
?>
|
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() {
const int n = 100;
int sum = 0;
for ( int i = 1; i < 2 * n; i += 2) {
sum += i;
}
cout << "Sum of first " << n << " terms of the sequence is: " << sum <<endl;
return 0;
}
|
Java
public class SumOfOddNumbers {
public static void main(String[] args)
{
final int n = 100 ;
int sum = 0 ;
for ( int i = 1 ; i < 2 * n; i += 2 ) {
sum += i;
}
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()
{
const int n = 100;
int sum = 0;
for ( int i = 1; i < 2 * n; i += 2) {
sum += i;
}
Console.WriteLine( "Sum of first " + n
+ " terms of the sequence is: "
+ sum);
}
}
|
Javascript
const n = 100;
let sum = 0;
for (let i = 1; i < 2 * n; i += 2) {
sum += i;
}
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’.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...