fork download
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main()
  5. {
  6. int x,i,n;
  7. double fact;
  8. double sum=0,k=1;
  9. x=-2;
  10. i=0; sum=0; k=1;
  11. while (fabs(k)>=1.0E-9) {
  12. int j;
  13. fact=1;
  14. for (j=1;j<=i;++j)
  15. fact*=j;
  16. k=pow(x,i)/fact;
  17. sum+=k;
  18. ++i;
  19. printf("%d,%f\n",i,k);
  20. }
  21. printf("%lf\n",sum);
  22. return 0;
  23. }
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
1,1.000000
2,-2.000000
3,2.000000
4,-1.333333
5,0.666667
6,-0.266667
7,0.088889
8,-0.025397
9,0.006349
10,-0.001411
11,0.000282
12,-0.000051
13,0.000009
14,-0.000001
15,0.000000
16,-0.000000
17,0.000000
18,-0.000000
0.135335