Printing Terms and Sum of geometric series x+x^2/2+..+x^n/n – CSKC
Sign In

Search

Solved Problem #CPP#2438    siteicon   siteicon  

Problem Statement - Printing Terms and Sum of geometric series x+x^2/2+..+x^n/n

Write a program to find the sum of the following series if x is 3 and n is 5. Also print the series terms.
x + x^2/2 + x^3/3 + …+x^n/n

Solution

TC++ #2438

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

  Related ( ? ) :

int x=3,n=5,y=0;
float sum=0;

x and n are declared initialised with user given values. y is used as increasing number for every iteration in the series. variable sum is used to calculate the sum of the series successively. The float type is used as the series expressions contain both numerator and denominators (Fractions).

for(int tc=1;tc<=n;++tc)
This loop starts from 1 and runs upto n as power term y starts from 1.
{
float term=pow(x,y)/y;
The power function and division by y is used to get the term of geometric progression. The output of RHS would be fraction so on RHS float type is declared.
cout<<term<<” “; //The term is printed here
sum+=term; //Sum is successively done in each iteration and the variable sum is updated.
y++; //The power term (exponent term) is increased so that it follow 0,1,2,3.. with each iteration.
}
cout<<endl<<“Sum of above series = “<<sum<<endl; //Sum is printed outside the loop

 

Common Errors

  • The term expressions which are of fraction types should necessarily be computed in float or double data type. If this is not done the integer division may either result in 0 or it may trim of fractional portion of the term.


Suggested Filename(s): geo-ser.cpp



Share

Created: 10-Jan-2018 19:47 | Updated: 18-Dec-2018 18:56|