Given the value of n, we need to find the sum of the series where i-th term is sum of first i natural numbers.
Examples :
Input : n = 5 Output : 35 Explanation : (1) + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) = 35 Input : n = 10 Output : 220 Explanation : (1) + (1+2) + (1+2+3) + .... +(1+2+3+4+.....+10) = 220
Naive Approach :
Below is implementation of above series :
C++
// CPP program to find sum of given series #include <bits/stdc++.h> using namespace std; // Function to find sum of given series int sumOfSeries(int n) { int sum = 0; for (int i = 1 ; i <= n ; i++) for (int j = 1 ; j <= i ; j++) sum += j; return sum; } // Driver Function int main() { int n = 10; cout << sumOfSeries(n); return 0; } |
Java
// JAVA Code For Sum of the series import java.util.*; class GFG { // Function to find sum of given series static int sumOfSeries(int n) { int sum = 0; for (int i = 1 ; i <= n ; i++) for (int j = 1 ; j <= i ; j++) sum += j; return sum; } /* Driver program to test above function */ public static void main(String[] args) { int n = 10; System.out.println(sumOfSeries(n)); } } // This code is contributed by Arnav Kr. Mandal. |
Python
# Python3 program to find sum of given series # Function to find sum of series def sumOfSeries(n): return sum([i*(i+1)/2 for i in range(1, n + 1)]) # Driver Code if __name__ == "__main__": n = 10 print(sumOfSeries(n)) |
C#
// C# Code For Sum of the series using System; class GFG { // Function to find sum of given series static int sumOfSeries(int n) { int sum = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) sum += j; return sum; } /* Driver program to test above function */ public static void Main() { int n = 10; Console.Write(sumOfSeries(n)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find // sum of given series // Function to find // sum of given series function sumOfSeries($n) { $sum = 0; for ($i = 1 ; $i <= $n ; $i++) for ($j = 1 ; $j <= $i ; $j++) $sum += $j; return $sum; } // Driver Code $n = 10; echo(sumOfSeries($n)); // This code is contributed by Ajit. ?> |
Output :
220
Efficient Approach :
Let
term of the series 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4)…(1 + 2 + 3 +..n) be denoted as an
an = Σn1=
=
Sum of n-terms of series Σn1 an = Σn1
=
Σ
![]()
![]()
+ Σ
![]()
![]()
=
*
+
*
=
![]()
Below is implementation of above approach :
C++
// CPP program to find sum of given series #include <bits/stdc++.h> using namespace std; // Function to find sum of given series int sumOfSeries(int n) { return (n * (n + 1) * (2 * n + 4)) / 12; } // Driver Function int main() { int n = 10; cout << sumOfSeries(n); } |
Java
// JAVA Code For Sum of the series import java.util.*; class GFG { // Function to find sum of given series static int sumOfSeries(int n) { return (n * (n + 1) * (2 * n + 4)) / 12; } /* Driver program to test above function */ public static void main(String[] args) { int n = 10; System.out.println(sumOfSeries(n)); } } // This code is contributed by Arnav Kr. Mandal. |
Python
# Python program to find sum of given series # Function to find sum of given series def sumOfSeries(n): return (n * (n + 1) * (2 * n + 4)) / 12; # Driver function if __name__ == '__main__': n = 10 print(sumOfSeries(n)) |
C#
// C# Code For Sum of the series using System; class GFG { // Function to find sum of given series static int sumOfSeries(int n) { return (n * (n + 1) * (2 * n + 4)) / 12; } /* Driver program to test above function */ public static void Main() { int n = 10; Console.Write(sumOfSeries(n)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find // sum of given series // Function to find // sum of given series function sumOfSeries($n) { return ($n * ($n + 1) * (2 * $n + 4)) / 12; } // Driver Code $n = 10; echo(sumOfSeries($n)); // This code is contributed by Ajit. ?> |
Output :
220
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:
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . .
- Sum of the series 1.2.3 + 2.3.4 + ... + n(n+1)(n+2)
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1))
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- Sum of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + …… + (2+4+6+8+….+2n)
- Sum of series (n/1) + (n/2) + (n/3) + (n/4) +.......+ (n/n)
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n
- Sum of Series (n^2-1^2) + 2(n^2-2^2) +....n(n^2-n^2)
- Sum of the series 3, 20, 63, 144, ......
- Sum of series 1*1*2! + 2*2*3! + ……..+ n*n*(n+1)!
- Sum of series 1*1! + 2*2! + ……..+ n*n!
- Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ....... (K-1)n
- Find the sum of the series x(x+y) + x^2(x^2+y^2) +x^3(x^3+y^3)+ ... + x^n(x^n+y^n)
- Sum of series M/1 + (M+P)/2 + (M+2*P)/4 + (M+3*P)/8......up to infinite
- n-th term of series 1, 17, 98, 354......
- Sum of the series 5+55+555+.. up to n terms
- N-th term in the series 1, 11, 55, 239, 991,....
- Sum of the series 1^1 + 2^2 + 3^3 + ..... + n^n using recursion
- Program for sum of cos(x) series
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.
Improved By : jit_t

