- 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
Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms in C++
In this problem, we are given an integer value N. Our task is to find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms.
Let's take an example to understand the problem,
Input : N = 3 Output : 6
Explanation −
12 - 22 + 32 = 1 - 4 + 9 = 6
Solution Approach
A simple approach to solve the problem is using loops. We will loop from 1 to n with iterator i.
If i is odd, add (i2) to the sum.
If i is even, subtract (i2) to the sum. At last, return the sum of series after the loop.
Algorithm
Initialise − sum = 0.
Step 1 −Loop till n, i -> 1 to n
Step 1.1 − if i is odd, add (i2) to sum, if (i % 2 == 0) => sum += i2
Step 1.2 − if i is even, add (i2) to sum, if (i % 2 == 0) => sum -= i2
Step 2 − Return sum.
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findSumOfSeries(int n) { int sum = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) sum -= (i*i); else sum += (i*i); } return sum; } int main(void) { int n = 5; cout<<"The sum of series is "<<findSumOfSeries(n); }
Output
The sum of series is 15
Another approach is using formulas for the sum of series.
If N is even,
$\mathrm{sum\:=\:1^2\:-\:2^2\:+\:3^2\:-\:4^2\:+\:\dotsm\:+\:(n-1)^2\:-n^2}$
$\mathrm{sum\:=\:(1-2)(1+2)\:+\:(3-4)(3+4)\:+\:\dotsm(n-1-n)(n-1+n)}$
$\mathrm{sum\:=\:(-1)(3)\:+\:(-1)(7)\:+\:(-1)(2n-1)}$
$\mathrm{sum\:=\:(-1)(1+2+3+4+\dotsm\:+(n-1)+n)}$
$\mathrm{sum\:=\:(-1)\:*\:\begin{bmatrix}\frac{n*(n+1)}{2} \end{bmatrix}}$
If N is odd,
$\mathrm{sum\:=\:1^2\:-\:2^2\:+\:3^2\:-\:4^2\:+\:\dotsm\:+\:(n-2)^2\:-(n-1)^2\:+\:n^2}$
$\mathrm{sum\:=\:(1^2\:-\:2^2\:+\:3^2\:-\:4^2\:+\:\dotsm\:+\:(n-2)^2\:-(n-1)^2)\:+\:n^2}$
$\mathrm{sum\:=\:\lbrace(-1)\:*[\frac{n*(n+1)}{2}]\rbrace\:+\:n^2}$
$\mathrm{sum\:=\:(\frac{-n^2\:+\:n\:+\:2n^2}{2})}$
$\mathrm{sum\:=\:(n+n^2)/2\:=\:n^*(n+1)/2}$
Example
Program to illustrate the working of our solution
#include <iostream> using namespace std; int findSumOfSeries(int n) { int sum = 0; if(n % 2 == 0){ sum = (-1)*(n*(n+1))/2; } else { sum = (n*(n+1))/2; } return sum; } int main(void) { int n = 5; cout<<"The sum of series is "<<findSumOfSeries(n); }
Output
The sum of series is 15
- Related Articles
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- C++ 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! +…….+ n/n!
- Sum of series 2/3 – 4/5 + 6/7 – 8/9 + …… upto n terms
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + ... in C++\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!
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
- Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^n in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Find the sum:( 4-frac{1}{n}+4-frac{2}{n}+4-frac{3}{n}+ldots ) upto ( n ) terms
- Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++
