Introduction
In this lab, we will learn how to write a C++ program that finds the sum of the series 1 + 2 + 4 + 8 + 16 + 32 + ... + n. We will use two different approaches. The first approach uses a for loop to add values in the sequence, while the second approach uses a mathematical formula to calculate the sum.
Create a new file
First, let's open the terminal and navigate to the ~/project directory. In this directory, create a new file called main.cpp. We can do this using the following command:
touch ~/project/main.cpp
Next, open the file in your favorite text editor.
Using a for loop
In this approach, we use a for loop to iterate through the sequence and add up the numbers from 1 to n. We will store the sum in a variable called sum.
Add the following code to your main.cpp file:
#include <iostream>
int main() {
int n, sum = 0;
std::cout << "Enter the value of n: ";
std::cin >> n;
for(int i = 1; i <= n; ++i) {
sum += i;
}
std::cout << "The sum of the sequence is: " << sum << std::endl;
return 0;
}
Here, we use the std::cout function to display a message asking the user to enter the value of n. We then use the std::cin function to read in the value of n entered by the user. We initialize sum to 0 before starting the for loop.
The for loop iterates over values of i from 1 to n, adding each value to the sum. Finally, we display the sum using std::cout.
Before we can run the program, we need to compile it. In the terminal, navigate to the ~/project directory and run the following command:
g++ main.cpp -o main && ./main
This will compile and run the program. You should see a message asking you to enter the value of n. After you enter a value, the program will display the sum of the sequence.
Using a mathematical formula
In this approach, we use a mathematical formula to calculate the sum of the sequence. The formula is sum = 2^n - 1, where n is the number of terms in the sequence.
Add the following code to your main.cpp file:
#include <iostream>
#include <cmath>
int main() {
int n;
std::cout << "Enter the value of n: ";
std::cin >> n;
int sum = pow(2, n) - 1;
std::cout << "The sum of the sequence is: " << sum << std::endl;
return 0;
}
Here, we use the std::pow function to calculate 2^n. We subtract 1 from this value to get the sum of the sequence. We then display the sum using std::cout.
Before we can run the program, we need to compile it. In the terminal, navigate to the ~/project directory and run the following command:
g++ main.cpp -o main && ./main
This will compile and run the program. You should see a message asking you to enter the value of n. After you enter a value, the program will display the sum of the sequence.
Full code
Here is the full code for main.cpp:
#include <iostream>
#include <cmath>
int main() {
int n;
std::cout << "Enter the value of n: ";
std::cin >> n;
int sum = pow(2, n) - 1;
std::cout << "The sum of the sequence is: " << sum << std::endl;
return 0;
}
#include <iostream>
int main() {
int n, sum = 0;
std::cout << "Enter the value of n: ";
std::cin >> n;
for(int i = 1; i <= n; ++i) {
sum += i;
}
std::cout << "The sum of the sequence is: " << sum << std::endl;
return 0;
}
Summary
In this lab, we learned how to write a C++ program to find the sum of the series 1 + 2 + 4 + 8 + 16 + 32 + ... + n. We used two different approaches - one using a for loop and another using a mathematical formula. You can use either of these methods to find the sum of any arithmetic sequence.



