Introduction
In this lab, we will learn how to write a C++ program to find the sum of a series of numbers. We will provide two methods to solve the problem and explain the code for each method.
In this lab, we will learn how to write a C++ program to find the sum of a series of numbers. We will provide two methods to solve the problem and explain the code for each method.
We will create a new file named main.cpp
in the ~/project
directory using the following command:
touch ~/project/main.cpp
First, we need to include the necessary libraries in our program to perform the required operations. In this case, we need iostream
to handle input and output operations and cmath
to perform mathematical operations.
#include <iostream>
#include <cmath>
using namespace std;
In the main function, we will ask the user for the value of x
and the number of terms in the series, n
.
int main() {
int x, n;
cout << "Enter the value of x: ";
cin >> x;
cout << "Enter the number of terms in the series, n: ";
cin >> n;
// code to calculate the sum of the series
return 0;
}
For the first method, we will use a for
loop to iterate through the given number of terms in the series. We will initialize the sum with 1 (the first term in the series) and multiply x
with itself for each subsequent term in the series, while adding the value to the sum.
double sum = 1;
for (int i = 1; i < n; i++) {
sum += pow(x, i);
}
Here, we are using the pow()
function from the cmath
library to calculate the value of x
to the power of each term in the series. We then add this value to sum
.
For the second method, we will use a for
loop to iterate through the given number of terms in the series. We will initialize the sum with 1 (the first term in the series) and then multiply x
with itself for each subsequent term. We will use a separate variable term
to calculate the value of each term in the series.
double sum = 1;
double term = 1;
for (int i = 1; i < n; i++) {
term *= x;
sum += term;
}
Here, we are using the *=
operator to multiply term
with x
and assigning the value back to term
. We then add the value of term
to sum
.
We will output the final result for the sum of the series using the following code.
cout << "The sum of the series is: " << fixed << sum << endl;
Here, fixed
is used for displaying the output in decimal format, and endl
is for printing a newline character after the output.
The lab will be completed in the ~/project/main.cpp
file and can be compiled and run in the terminal of an Ubuntu system using the following command:
g++ main.cpp -o main && ./main
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x, n;
cout << "Enter the value of x: ";
cin >> x;
cout << "Enter the number of terms in the series, n: ";
cin >> n;
// Method 1
// double sum = 1;
// for (int i = 1; i < n; i++) {
// sum += pow(x, i);
// }
// Method 2
double sum = 1;
double term = 1;
for (int i = 1; i < n; i++) {
term *= x;
sum += term;
}
cout << "The sum of the series is: " << fixed << sum << endl;
return 0;
}
In this lab, we have learned how to write a C++ program to find the sum of a series of numbers. We have also provided two methods to solve the problem and explained the code for each method. The user can select any method to get the desired result based on the requirement. With the help of this lab, the readers will learn to implement the code for pattern or sum calculation in C++ programming.