
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
In this problem, we are given a number n of the series. Our task is to find the sum of series 1^2 + 3^2 + 5^2 +... + (2*n - 1)^2 for the given value of n.
Let’s take an example to understand the problem,
Input −
n = 5
Output −
84
Explanation −
sum = 1^2 + 3^2 + 5^2 + 7^2 + 9^2 = 1 + 9 + 25 + 49 = 84
A basic approach to solve this problem is by directly applying the formula for the sum of series.
Example
#include <iostream> using namespace std; int calcSumOfSeries(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum += (2*i-1) * (2*i-1); return sum; } int main() { int n = 5; cout<<"The sum of series up to "<<n<<" is "<<calcSumOfSeries(n); return 0; }
Output
The sum of series up to 10 is 165
Another approach to solve is to use the mathematical formula to find the sum of the series.
The sum is,
1^2 + 3^2 + 5^2 + … + (2*n - 1)^2 = {(n * (2*(n-1)) * (2*(n+1)))/3}
Example
#include <iostream> using namespace std; int calcSumOfSeries(int n) { return (n * (2 * n - 1) * (2 * n + 1)) / 3; } int main() { int n = 5; cout<<"The sum of series up to "<<n<<" is "<<calcSumOfSeries(n); return 0; }
Output
The sum of series up to 5 is 165
- Related Articles
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Examine whether root 2 plus 2 square is rational or irrational
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- 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/(3*4) + 1/(4*5) + ... in C++\n
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms in C++
- Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^n in C++
- Sum of the series 1^1 + 2^2 + 3^3 + ... + n^n using recursion in C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Plus One in Python
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
- Prove that:\( \frac{2^{n}+2^{n-1}}{2^{n+1}-2^{n}}=\frac{3}{2} \)
- LocalDateTime plus() method in Java

Advertisements