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

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

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
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);
}



Post a Comment

Have any Doubt? Ask Here!