Write a program to compute the sum up to n term of the following series: S=1-x+x^2/2-x^4/24+……..+(-1)^n X^/n!

#include<stdio.h>
#include<conio.h>
#include<math.h>
int factorial(int n);
void main ()
{
int i,x,n,sign=1;
float sum=0,term;
clrscr();
printf(“Enter your X and N number”);
scanf(“%d%d”,&x,&n);
for(i=0;i<=n;i++)
{
term=sign * pow(x,i)/(1.0*factorial(i));
sum=sum+term;
sign=-sign;
}
printf(“%f”,sum);
getch();
}
int factorial(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return f;
}

After Running Program:)

 

 

 

 

Leave a Reply