Thursday, 28 June 2012

c++ program that calculate the sum of : x-x2/2!+x3/3!-x4/4!+…………….n terms

#include<iostream.h>

int main()
{
int i,n,j,fa=1,sq=1;
float ans=0.0,sum=0.0;

cout<<"Enter any number : ";
    cin>>n;
for(i=1;i<=n;i++)
{



    for(j=1;j<=i;j++)
    {
        fa=fa*j;
        sq=sq*i;
    }
    ans=(float)sq/(float)fa;

    if(i%2!=0)
        sum+=ans;
    else
        sum-=ans;
    fa=1;
    sq=1;
}

cout<<sum;

}