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