
- 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^0 + 2^1 + 2^2 +...+ 2^n in C++
In this problem, we are given a number n which defines the n-th term of the series 2^0, 2^1, 2^2, …, 2^n. Our task is to create a program to find the sum of the series 2^0 + 2^1 + 2^2 +...+ 2^n.
Let’s take an example to understand the problem,
Input
n=6
Output
Explanation
sum = 2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 sum = 1 + 2 + 4 + 8 + 16 + 32 + 64 = 127
A simple solution to the problem is by using the loop. Finding the 2^i, for each value from 0 to n and add it to the sum variable.
Algorithm
Initialize sum = 0 Step 1: Iterate from i = 0 to n. And follow : Step 1.1: Update sum, sum += 2^i. Step 2: Print sum.
Example
Program to illustrate the working of our solution,
#include <iostream> #include <math.h> using namespace std; int calcSeriesSum(int n) { int sum = 0; for (int i = 0; i <= n; i++) sum += pow(2, i); return sum; } int main() { int n = 11; cout<<"Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^"<<n<<" is "<<calcSeriesSum(n); return 0; }
Output
Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^11 is 4095
This is not the most effective method to solve this problem as it uses a loop that makes its time complexity of the order O(n).
A more effective solution, we will use the mathematical formula for the sum. It is given by
2^(n+1) - 1
Example
Program to illustrate the working of our solution,
#include <iostream> #include <math.h> using namespace std; int calcSeriesSum(int n) { return ( (pow(2, (n+1)) - 1) ); } int main() { int n = 11; cout<<"Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^"<<n<<" is "<<calcSeriesSum(n); return 0; }
Output
Sum of the series 2^0 + 2^1 + 2^2 +...+ 2^11 is 4095
- Related Articles
- Examine whether root 2 plus 2 square is rational or irrational
- Plus One in Python
- 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++
- 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++
- Find Sum of Series 1^2 - 2^2 + 3^2 - 4^2 ... upto n terms in C++
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + … + upto n terms in C++
- Cplus plus vs Java vs Python?
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
- What is the Cost plus pricing method?

Advertisements