Sunday, 7 March 2010

Write a C program to find a sum of series:S=1+x/1!+x2/2!+........…


Write a C program to find a sum of series:S=1+x/1!+x2/2!+........…
#include <stdio.h>
#include <math.h>

int factorial(int n)
{
if(n==1)
return 1;
else
return (n * factorial(n-1));
}

int main()
{
int x,i;
double S=1;

printf("Please input x : ");
scanf("%d", &x); fflush(stdin);

for(i=1; i<=x; i++)
{
S += pow(x,i)/ factorial(i);
}
printf("%lf", S);
getchar();
return 0;
}

No comments:

Post a Comment