The question is incomplete! Here is the complete question and it’s answer!
Question:
For this lab, you have to write a programnamedseriesFunction.cthat prompts user to enter two values, x and n. Then it would use a function to compute the sum of the series. It will take two arguments, x and n. It will return the sum of the series.
S(x) = x + (x+1) + (x+2) + (x+3) + (x+4) +........................+(x + n - 1) + (x + n)
As an example, for input values x = 100 and n = 0, it should output 100. For x = 100 and n = 1, it should output 100 + 101 = 201.
Step by Step Explanation:
We are required to find the sum of the following series.
S(x) = x + (x+1) + (x+2) + (x+3) + (x+4) +........................+(x + n - 1) + (x + n)
We have created a function seriesFunction which takes two inputs x and n and calculates the sum of the series (x+n). A for loop is used to run n times and a sum variable is used to keep adding n number of times.
Then we have driver code in the main function where we ask the user to enter x and n values and then called the seriesFunction to calculate the sum of series
Code:
#include <iostream>
using namespace std;
int seriesFunction(int x, int n)
{
int sum;
for(int i = 0; i <= n; i++)
sum = sum + (x+i);
cout<<"sum of series: "<<sum<<endl;
}
int main()
{
int x,n;
cout << "Enter x"<<endl;
cin >> x;
cout << "Enter n"<<endl;
cin >> n;
seriesFunction(x,n);
return 0;
}
Output:
Test 1:
Enter x
100
Enter n
1
sum of series: 201
Test 2:
Enter x
100
Enter n
4
sum of series: 510