
- 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 the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + ... + (2+4+6+8+...+2n) in C++
In this problem, we are given a number n which defines the nth term of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + ... + (2+4+6+8+...+2n). Our task is to create a program to find the sum of the series.
Let’s take an example to understand the problem,
Input
n = 3
Output
Explanation − sum = (2) + (2+4) + (2+4+6) = 2 + 6 + 12 = 20
A simple solution to the problem is to use a nested loop. The inner loop finds the ith element of the series and then add up all elements to the sum variable.
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++) { int even = 2; for (int j = 1; j<=i; j++) { sum += even; even += 2; } } return sum; } int main() { int n = 5; cout<<"Sum of the series 2 + (2+4) + (2+4+6) + ... + (2+4+6+...+"<<(2*n)<<") is "<<calcSeriesSum(n); return 0; }
Output
Sum of the series 2 + (2+4) + (2+4+6) + ... + (2+4+6+...+10) is 70
This is not the most effective way to solve the problem as the time complexity of the problem is of the order O(n2).
An effective solution to the problem is by using the mathematical formula for the sum of the series.
The series is 2 + (2+4) + (2+4+6) + (2+4+6+8) + ... + (2+4+6+8+...+2n)
The nth term of the series is
an = (2 + 4 + 6 + 8 + … + 2n) = (n*n) + n
an is the sum of even numbers upto n.
The sum of the series is
sum = 2 + (2+4) + (2+4+6) + (2+4+6+8) + ... + (2+4+6+8+...+2n) sum = ∑ (n2 + n) sum = ∑ n2 + ∑ n sum = [ (n*(n+1)*(2n + 1))/6 ] + [ (n*(n+1))/2 ] sum = ½ (n*(n+1)) [(2n + 1)/3 + 1] sum = ½ (n*(n+1)) [(2n + 1 + 3)/3] sum = ½ (n*(n+1)) [2(n+2)/3] sum = ⅓ n*(n+1)(n+2)
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int calcSeriesSum(int n) { return ((n)*(n+1)*(n+2)/3); } int main() { int n = 5; cout<<"Sum of the series 2 + (2+4) + (2+4+6) + ... + (2+4+6+...+"<<(2*n)<<") is "<<calcSeriesSum(n); return 0; }
Output
Sum of the series 2 + (2+4) + (2+4+6) + ... + (2+4+6+...+10) is 70
- 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
- Plus One Linked List in C++
- Cplus plus vs Java vs Python?
- What is the Cost plus pricing method?
- What is Unary Plus Operator in JavaScript?
- What are the specifications of one plus 5?
- C Program to check Plus Perfect Number
- Now call facebook stream to google plus
- Intermittent Fasting Plus Keto for Weight Loss
- What are the specifications of one plus 3T phone?
- Examine whether root 2 plus 2 square is rational or irrational
