Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Question about estimating the value of mathematical constant e

843789Jan 16 2010 — edited Jan 17 2010
Hello all,

I'm writing an application to estimate the value of the mathematical constant e by using the formula:
e=1+(1/1!)+(1/2!)+(1/3!) +... [There are plus signs between (1/1!), (1/2!) and (1/3!). System seems to omit them]
n! = n *(n-1)*(n-2)*...1
0!=1

It seems like an infinite loop for completing the calculation. It cannot come out with a result. Could anyone help me? Thank you very much! Below is what I have written:*


public class Exercise
{

    public static void main(String args[])
    {
        int x1=1;
        int x2=1;
        double fac=1;  //fac represents factorial
        double e=1;

        while (x1 > 0)
        {
            x2=x1;
            fac=1;
            while (x2 >= 1)
            {
                fac = fac * x2;
                x2 -= 1;
            }
            fac = 1/fac;
            e = e + fac;
            x1 += 1;
        }

        System.out.printf("the estimated value of e is %f", e);
    }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 14 2010
Added on Jan 16 2010
4 comments
968 views