#### How to fix the code for “ e = 1 + 1/1! + 1/2! + 1/3! + … + 1/n! ”?

``````#include <stdio.h>

int main (void)
{
int n, i, j;
float e = 1.0, nFact = 1.0;

scanf ("%d", &n);

for (i = 1; i <= n ; i++)
{
for (j = 1; j <= i; j++)
{
nFact *= j;
}
e = e + (1.0 / nFact);
}

printf ("The value of 'e' is : %f", e);
return 0;
}
``````

3 个回复

``````#include<stdio.h>

int main (void)
{
int n,i,j;
float e=1.0, nFact=1;

scanf("%d", &n);

for( i =1; i<= n ; i++)
{
nFact*=i;
e = e + (1.0/ nFact);
}

printf("The value of 'e' is : %f", e);

return 0;
}
``````

`Am i doing something wrong here?`

``````nFact=1.0;     //add this line so it resets to 1.0 everytime a factorial is needed
for( j = 1  ; j <= i; j++)
{
nFact *= j;
e = e + (1.0/ nFact);
}
//only single loop is more than enough
``````

`how can i get the proper output?`

``````double term = 1;
double result = term;

for (unsigned int i = 1; i != n; ++i)
{
term /= i;
result += term;
}

printf("With %u steps we compute %f.\n", n, result);
``````
1 系列：1 + 1/3 + 1/5 +...最多 N 项

2019-10-16 08:12:03 2 338   c/ series
4 1.307与系列1 + 1/2 + 1/3 + 1/4…+ 1 / n有什么关系[关闭]

2018-07-16 05:47:56 3 119   c#/ math
5 如何修复此e2e测试？

7 如何使1 +1 = 2而不是1 +1 = 11 [重复]

2015-12-14 10:15:36 5 2316   jquery
8 如何修复 E/ExoPlayerImplInternal: 源错误

9 我为什么要修复E_NOTICE错误？

2011-02-22 03:15:49 5 8409   php
10 修复代码

2015-09-03 02:49:56 2 39   ionic