-
value of 'e'
i'm still a beginner and i supposed to write this program to find the value of 'e'.
this is the constant e : http://upload.wikimedia.org/math/a/1...d2dd287fdb.png
e is the some of infinite series. the value are suppose to show 2.71828.... but my program shows 2.586834. where did i messed up ? this is how i do so far :
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
int counter;
float value1;
float var1 = 0;
int var2 = 1;
int var4 = 0;
float var3 = 1;
float var5;
float result = 0;
for ( counter = 0; counter <= 50; counter++ ) {
value1 = var4;
while ( value1 > 0 ) { // loop for the factorial
var1 = var2 * value1--;
var3 = var3 * var1;
} // end of factorial loop
var5 = 1.0 / var3;
result = result + var5;
var4++;
}
printf ("the result is %f", result);
getchar ();
return 0;
}
-
You are computing the factorial wrong... each time through the outer for loop, you do not reset var3 to 1 like it seems you should, it gets really huge far faster than it should. If you had printed out var3 after the "factorial" loop you could have easily seen that these values were wrong... this is part of the basic debugging process. So, the quick solution is to add a var3 = 1 statement before the "factorial" loop.