- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
In this problem, we are given a number n. Our task is to create a program to find the sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + … + (1+2+3+4+...+n).
Lets example to understand the problem,
Input
n = 4
Output
20
Explanation −(1) + (1+2) + (1+2+3) + (1+2+3+4) = 20
A simple solution to the problem will be creating the series by using two loops.
Algorithm
Initialize sum = 0 Step 1: Loop for i -> 1 to n i.e i = 1 to i <= n. Step 1.1: Loop for j -> 1 to i i.e. i = 1 to i <= i. Step 1.1.1: update sum i.e. sum += j. Step 2: return sum.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int calcSeriesSum(int n) { int sum = 0; for (int i = 1 ; i <= n ; i++) for (int j = 1 ; j <= i ; j++) sum += j; return sum; } int main() { int n = 7; cout<<"Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+"<<n<<") is "<<calcSeriesSum(n); return 0; }
Output
Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+7) is 84
But this approach is not effective.
An effective solution could be deriving the general formula for finding the sum of the series.
sum = 1 + (1+2) + (1+2+3) + (1+2+3+4) … sum = ∑ ( (1+2+3+4+5+...) ) sum = ∑ ( n(n+1)/2) sum = ½ ∑ ( n^2 + n) = ½ (∑ (n2) + ∑ n) sum = ½ [ (n(n+1)(2n+1))/6 ) + ½ ( n(n+1)/2 ] sum = ½ [ (n(n+1))/2 ( (2n+1)/3 + 1) ] sum = ½ [ ((n(n+1))/2) * (2n + 1 + 3)/3 ] sum = ½ [ (n(n+1)(2n+4))/6] sum = (n(n + 1)(2n + 4))/6
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int calcSeriesSum(int n) { return (n*(n + 1)*(2*n + 4))/12; } int main() { int n = 7; cout<<"Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+"<<n<<") is "<<calcSeriesSum(n); }
Output
Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+7) is 84
- Related Articles
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- C++ program to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- C++ Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! + …… n/n!
- Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +…….+ n/n!
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n in C++
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++
- Observe the following pattern$1=frac{1}{2}{1 times(1+1)}$$1+2=frac{1}{2}{2 times(2+1)}$$1+2+3=frac{1}{2}{3 times(3+1)}$$1+2+3+4=frac{1}{2}{4 times(4+1)}$and find the values of each of the following:(i) $1 + 2 + 3 + 4 + 5 +….. + 50$(ii)$31 + 32 +… + 50$
- Observe the following pattern( 1^{2}=frac{1}{6}[1 times(1+1) times(2 times 1)+1)] )( 1^{2}+2^{2}=frac{1}{6}[2 times(2+1) times(2 times 2)+1)] )( 1^{2}+2^{2}+3^{2}=frac{1}{6}[3 times(3+1) times(2 times 3)+1)] )( 1^{2}+2^{2}+3^{2}+4^{2}=frac{1}{6}[4 times(4+1) times(2 times 4)+1)] )and find the values of each of the following:(i) $1^2 + 2^2 + 3^2 + 4^2 +…………… + 10^2$(ii)$5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2$
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms in C++

Advertisements