
- 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
Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++
In this problem, we are given a number n that denotes that nth term of the series. Our task is to create a Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n in C++.
Problem description − Here, we will find the sum of series whose nth terms is n times the sum of number n. This means it is a series of square numbers.
Let’s take an example to understand the problem
Input
n = 4
Output
30
Explanation
Sum of series till 4th term = 1 + 2 + 2 + 3 + 3 + 3 + 4 + 4 + 4 + 4 = 30
Solution Approach
The most effective solution to the problem would be using the general formula for the sum of series.
But let’s discuss all possible solutions of the problem that one might think of.
The simplest solution to the problem is directly by adding numbers of series till n. This will require two nested loops, one of term and the inner one for the values in each term.
Algorithm
Initialize − sumVar = 0;
- Step 1 − Loop for i -> 1 to n.
- Step 1.1 − Loop for j -> 1 to i.
- Step 1.1.1 − Update sumVar, sumVar+=i;
- Step 1.1 − Loop for j -> 1 to i.
- Step 2 − Print sumVar.
Program to illustrate the working of our solution
Example
#include <iostream> using namespace std; int calcSeriesSum(int n){ int sumVar = 0; for(int i = 1; i <= n; i++){ for(int j = 1; j <= i; j++){ sumVar += i; } } return sumVar; } int main(){ int n = 7; cout<<"The sum of series till "<<n<<" is "<<calcSeriesSum(n); return 0; }
Output
The sum of series till 7 is 140
This solution is simple but is not effective as it has two nested loops making its time complexity of the order O(n2).
An Effective solution is based on the fact that if a number(n) is added to itself n times. Then, the result can be achieved by multiplying the number with itself.
i.e. 5+5+5+5+5 = 5*5.
So, we can use the multiplication instead of one loop to solve the problem.
Algorithm
Initialize − sumVal = 0;
- Step 1 − loop for i -> 0 to n.
- Step 2 − update sumVal, sumVal += (i*i)
Program to illustrate the working of our solution
Example
#include <iostream> using namespace std; int calcSeriesSum(int n){ int sumVar = 0; for(int i = 1; i <= n; i++){ sumVar += (i*i); } return sumVar; } int main(){ int n = 7; cout<<"The sum of series till "<<n<<" is "<<calcSeriesSum(n); return 0; }
Output
The sum of series till 7 is 140
The solution is better as it takes only one loop and has a time complexity of the order O(n). But it isn’t the best possible solution as the same can be done in O(1) time complexity.
The most effective solution is using a general formula for the sum of the given series.
Sum of series =
1 + 2 + 2 + 3 + 3 + 3 + …. N.
This can be made as
1 + (2+2) + (3+3+3) + … + (N+N+N..N) 1*1 + 2*2 + 3*3 + … N*N. 12 + 22 + 32 + … N2.
The formula for the sum of squares is n*(n+1)*(2n+1)/6. And we can find the sum using this formula too.
Program to illustrate the working of our solution
Example
#include <iostream> using namespace std; int calcSeriesSum(int n){ int sumVar = 0; sumVar = ((n*(n + 1)*( 2 * n + 1))/6 ); return sumVar; } int main(){ int n = 7; cout<<"The sum of series till "<<n<<" is "<<calcSeriesSum(n); return 0; }
Output
The sum of series till 7 is 140
- Related Articles
- Plus One in Python
- LocalDateTime plus() method in Java
- Duration plus() method in Java
- LocalTime plus() method in Java
- Instant plus() method in Java
- LocalDate plus() method in Java
- C Program to check Plus Perfect Number
- Plus One Linked List in C++
- Cplus plus vs Java vs Python?
- Now call facebook stream to google plus
- What is Unary Plus Operator in JavaScript?
- What is the Cost plus pricing method?
- Intermittent Fasting Plus Keto for Weight Loss
- Examine whether root 2 plus 2 square is rational or irrational
- What are the specifications of one plus 5?
- MySQL query to insert current date plus specific time?
