Signup/Sign In

C++ Program To Find Sum Of Series x + x ^ 2 / 2 + x ^ 3 / 3 + x ^ 4 / 4 + . . . . . . . . . x ^ n / n

Here, in this tutorial, we will be seeing how to write the program for the given pattern and at the end print the resultant sum of the series formed for the input number of terms by the user.

C++ Program To Print The Sum of The Pattern

Before moving towards the program let's first see the approach for solving this problem.

This is a very simple series just take an input a value of 'X' and numbers of terms and calculate the sum .at the end print the sum the logic is given below.

for(i=1;i<=n;++i){

sum+=pow(x,i)/i;

}

Also here we will take the value of sum as a float datatype so that our answer could be more accurate and correct.

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
 int i,n;
 float x,sum=0;

 cout<<"\nx+x^2/2+x^3/3+…..+x^n/n\n";
 cout<<"\nEnter value of x and n :\n";
 cin>>x>>n;

 for(i=1;i<=n;++i)
 {
  sum+=pow(x,i)/i;
 }
 cout<<"\nSum is = "<<sum<<endl;
 return 0;
}


x+x^2/2+x^3/3+…..+x^n/n

Enter value of x and n :
2 3

Sum is = 6.66667

Conclusion

That sums up this article detailing one program, each in C++, to find the sum of the series x + x2/2 +x3/3 + ……. +xn/n. There can be different programs for calculating for other variations of the series.