C++ Programme to sum the sequence x -(x^2/2!) + (x^4/4!) -(x^6/6!) + .......



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

int main()
{
                int x,p,i,j;
                double fact=1.0,ans=0;
                cout<<"Enter the value of x:";
                cin>>x;
                cout<<"Enter till what power you want:";
                cin>>p;
                ans=x;
                for(i=2,j=1;i<=p;i++,j++){
                                fact=fact*i;
                                if(i%2==0)
                                                ans+=(pow(-1,j))*((pow(x,i))/(fact));
                }
                cout<<"The sum of the series is:"<<ans;
                return 0;
}


OUTPUT:

Enter the value of x:3
Enter till what power you want:4                                                
The sum of the series is:-4.875

No comments:

Post a Comment