# calculating e

• 06-10-2011
roelof
calculating e
hello,

I have to calculate the mathetical e.
So I have this :
Code:

``` */ #include        <stdio.h>         int main ( int argc, char *argv[] ) {         int n, teller ;         float  uitkomst=0;         printf ("Enter n : ");         scanf ("%d", &n);         printf( "n is : %d ", n);         for (teller=1; teller <=n; teller++)                 {                         uitkomst = 1 / teller;                         printf (" uitkomst  is : %f \n", uitkomst);                 }         printf ("e is ongeveer : %f", uitkomst); return 0 ; }                              /* ----------  end of function main  ---------- */```
But the rule uitkomst = 1/ teller gives as output always zero and not 1/2 , 1/3

What am I doing wrong now.

Roelof
• 06-10-2011
mike65535
You'll find out the hard way that c really sucks when you try to mix integers and floats.

Try this:

Code:

`uitkomst = 1.0 / teller;`
• 06-10-2011
brewbuck
That algorithm does not compute e, don't understand why you think it does
• 06-10-2011
mike65535
I'm hoping he simplified it to demonstrate the issue he was having with mixing floats and ints.
• 06-10-2011
roelof
Hello,

I have simplified things because they don't work.

The right formula is : 1 + 1/1 + 1/(1*2) + 1/ (1*2*3) + 1/ (1*2*3...*n)

Roelof
• 06-10-2011
stahta01
Integer Division
Integer Division and Modulus

Use 1.0 instead of 1 as mentioned in mike65535 post.

Tim S.
• 06-10-2011
quzah
Code:

`e = d + 1`
That's how I calculate e.

Quzah.
• 06-10-2011
h_dog
This is not the way to calculate e, first of all you will end up with 1/n and you don't need the loop. You must first write a function that calculates the factorial of x " fact(x) " and then change:
uitkomst = 1 / teller;
to:
uitkomst += 1 / fact(teller);
• 06-10-2011
stahta01
Quote:

Originally Posted by h_dog
This is not the way to calculate e, first of all you will end up with 1/n and you don't need the loop. You must first write a function that calculates the factorial of x " fact(x) " and then change:
uitkomst = 1 / teller;
to:
uitkomst += 1 / fact(teller);

This is not the most efficient way; using a loop is better.
But, yours is a good and easier to understand way.
But you should use "1.0 /" instead of "1/"

Tim S.
• 06-10-2011
claudiu
I think you are better off calculating, or more likely, approximating the value of e from the definition of e: lim((1+1/n)^n) where n->infinity. This way you avoid calculating a ton of factorials for no reason. Once you get to a high enough n where you are starting to get the correct value of e, you should experiment with calculating the difference between f(n+1) - f(n) to see if the difference tends to be 0. (where f(n) = (1 + 1/n)^n )
• 06-10-2011
grumpy
Personally, if I had to compute e, I would do this
Code:

```#include <math.h> int main() {     double e = exp(1.0); }```
The precision is naturally limited to the machine precision of a double. But so is any other iterative approach that makes use of a double.
• 06-11-2011
roelof
Hello,

Everybody thanks for the help.
I have to use loops because im now reading the chapter about loops.

Roelof