value of 'e'

This is a discussion on value of 'e' within the C Programming forums, part of the General Programming Boards category; i'm still a beginner and i supposed to write this program to find the value of 'e'. this is the ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Jakarta
    Posts
    18

    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;
    	
    	}

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21