write a java program to add 2/1!+3/2!+4/3!+5/4!....... up to n terms please answer quickly.
Answers
Answered by
0
The number you are trying to create is e-1. e is the natural log base e. We can find the value in Java as Math.E;
To write a program to calculate it:
public class E{
public static double epsilon(int n){ // n >= 1
int fact=1; double sum=0;
for (int i=1; i<=n; i++){
fact = fact * i;
sum += (double) 1/fact;
}
return sum+1; // add the 1/0! into the sum to create natural exponential e
}
public static void main(String[] args){
System.out.println("e="+epsilon(20)+" compared to "+Math.E);
}
}
You can take one out to get your sequence sum. You can also increase n to get high precision.
To write a program to calculate it:
public class E{
public static double epsilon(int n){ // n >= 1
int fact=1; double sum=0;
for (int i=1; i<=n; i++){
fact = fact * i;
sum += (double) 1/fact;
}
return sum+1; // add the 1/0! into the sum to create natural exponential e
}
public static void main(String[] args){
System.out.println("e="+epsilon(20)+" compared to "+Math.E);
}
}
You can take one out to get your sequence sum. You can also increase n to get high precision.
Similar questions