C++ Program to print following series using function: x + x^3/3! + x^5/5! +…….+ x^n/n!

CWC
1 Min Read

Program to print following series using function: x + x^3/3! + x^5/5! +…….+ x^n/n! in C++

#include
#include
using namespace std;
int main()
{
int x,n;
double sum(int,int);
double res;
cout<<"x + x^3/3! + x^5/5! +.......+ x^n/n!";
cout<<"\n\nEnter value of x and n:"; cin>>x>>n; res=sum(x,n);
cout<<"\nSum of series is "<<res; getch();
}
double sum(int a,int b)
{
long power(int,int);
int i,j;
double s=0,fac=1; long p;
for(i=1;i<=b;i+=2)
{
p=power(a,i);
for(j=1;j<=i;++j)
{
fac*=j;
}
s+=p/fac;  fac=1;
}
return(s);
}
long power(int x,int i)
{
long res=1,j;
for(j=1;j<=i;++j)
{
res*=x;
}
return(res);
} 
Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version