package homework1C;
public class Homework1C {
public static void main(String[] args){
double term =2,sum;
int n;
final double difference = 0.0000000001;
double x;
for(sum=0.0,n=0;term > difference;n++){
x = find_n_fact(n);
term=1.0/x;
sum+=term;
n++;
}
System.out.printf("e : %f\n", sum);
System.out.printf("term : %d\n", n);
}
public static int find_n_fact(int n){
int i;
int fact = 2;
for(i = n; i>2;i--){
fact *= i;
}
return fact;
}
}
this is what i was being asked to do : Write another Java application program to find and display an approximation of e (natural logarithm). Use the following approximation formula starting with n as 2, incrementing by 1 until two successive values of e differ by less than 0.0000000001 and display not only the approximation, but how many terms of n were used in the last approximation. The formula is: approximation of e = 1/0! + 1/1! + 1/2! + 1/3! + ... , where n! is n factorial
This is my present output for this program
e : 1.043081
term : 20
what am i doing wrong ? the answer was suppose to be
e: 2.71828
term: 15
How to solve this?