C++
REVISION TOUR
Q. Write a C++ function having two value parameters
X and N with result type float to find the sum of series given below:
1+X1/2!+X2/3!+………+XN/(N+1)!
Answer:
float
series(float X, int N)
{
float sum=0, term;
sum+=1;
int
fact, f;
for(int
i=1; i<=N; i++)
{
fact=1;
for(f=1;
f<=(i+1); f++)
fact*=f;
term=pow(X,
i)/fact;
sum+=term;
}
return
sum;
}
Q. Write a C++ function having two value
parameters U and n with result type float to find the sum of series given
below:
1-U+U2/2!-U3/3!+U4/4!..........(+/-)Un/n!
Answer:
float
series (float U, int n)
{
float sum=0, sign=+1, term=0;
sum+=1;
int I, j, fact;
for (i=1; i<=n; i++)
{
sign*=-1;
fact=1;
for(j=1; j<=I; j++)
fact*=j;
term =(sign*pow(U, i)/fact;
sum+=term;
}
return sum;
}
0 comments: