# [NEWBIE] Trying to find the value of 'e'

This is a discussion on [NEWBIE] Trying to find the value of 'e' within the C Programming forums, part of the General Programming Boards category; i'm still new at this, so i really need your help. basically i try to find the value of this ...

1. ## [NEWBIE] Trying to find the value of 'e'

i'm still new at this, so i really need your help.

basically i try to find the value of this equation :

Code:
`e = 1 + 1/1! + 1/2! + 1/3! +.......`
i already find the formula to find final the value of factorial :

Code:
```while ( value1 > 0) {

var1 = var2 * value1--;
result = result * var1;

}```
but i wasn't able to get the code to work. this is how i did so far :

Code:
```#include <stdio.h>
#include <math.h>
#include <stdlib.h>

main ()
{
int a, b = 0, c = 1, d = 0, e = 0, f = 1, D = 0;

while ( a > 0 ) { /* loop for the whole formula

do { /* loop for the factorial */

D = d;
b = c * d--;
f = f * b;
} while ( d > 0 );

a = 1 / f;
e = e + a;
D++;
}

printf ( "%d", e );
return 0;
}```
where did i messed up ? what should i change ? I already tried diffrent version, and this one is the closest i get... At lease that is what i think....

2. 1 / f is always zero. Maybe 1.0 / f is what you want; or better yet, make f a double value as well, since an int overflows at less than 20!. (Note also that you'll have to reset f to 1 before every time you start your do-while loop (not every run through the loop itself, but when you compute a different factorial).

3. Have you learned about for loops yet, prominababy? It may be a valid alternative to your factorial loop.

Example:
Code:
```int main(void)
{
int i, f[3] = {0,1,0};

for(i = 0; i < 20; i++)
{
f[2] = f[0] + f[1];
f[0] = f[1];
f[1] = f[2];
printf("&#37;d ", f[2]);
}

return 0;
}```
Obviously this is a Fibonacci sequence, not a factorial. But since everyone is complaining about answering questions directly as of late, I think its a suitable example of a for loop.

4. i already try with the looping with for. But the results ends up dapending on how many times i loop it. If i loop it for 3 times than the result is 3 and if i loop it for 4 times than the result is also 4. here is how i done so far, i'm sorry, not very tidy :

Code:
```#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int main ()
{

int a, b, c, d = 1, f = 0;
float g, h = 0, e = 1 ;

for ( a = 0; a <= 5; a++ ) {

f = b;

while ( b > 0 ) {

c = d * b--;
e = e * c;
}

g = 1.0 / e;
h = h + g;
f++;
}

printf ("the result is %.2f", h);
getchar ();
return 0;

}```

5. Not very tidy, indeed. At least your loop is much cleaner now, thank you. I saw someone mentioned the overflow issues with doing factorials, why have you not addressed them?

By the way, b is never initialized nor is it ever reset.

6. haha ! i found the mistake ! And it's a very annoying one...

all i have to do was change the f = b into b = f.

thx for the looping with for tip ! Cheers mate !

7. Example:
Code:
```int factorial(int base, float *result)
{
if(base < 13 && result)
{
for(*result = 1;base;--base)
*result *= base;
return 1;
}

return 0;
}

// usage
if(factorial(b, &e))
{
// do stuff
} else
{
fputs("Integer overflow!", stderr);
}```