import java.text.DecimalFormat; import java.util.Scanner; public class NaturalE
ID: 3661197 • Letter: I
Question
import java.text.DecimalFormat;
import java.util.Scanner;
public class NaturalE {
public static void main(String[] args) {
{
int number, //counter
accuracy, //accuracy of estimate
factorial = 1; // value of factorial
double e;
number = 1;
e = 1.0;
// keyboard input
Scanner keyboard = new Scanner (System.in);
//declaring format
DecimalFormat Formatter =
new DecimalFormat ( "0.00" );
System.out.println("Enter desired accuracy of e: ") ;
accuracy=keyboard.nextInt();
//calculate estimation
while (number < accuracy )
{
factorial *= number;
e += 1.0 / factorial;
number++;
}
System.out.print ("e is " +Formatter.format (e));
}
}
}
Explanation / Answer
import java.text.DecimalFormat;
import java.util.Scanner;
public class NaturalE {
public static void main(String[] args) {
{
double number, //counter
accuracy, //accuracy of estimate
factorial = 1; // value of factorial
double e;
number = 1;
e = 1.0;
// keyboard input
Scanner keyboard = new Scanner (System.in);
//declaring format
DecimalFormat Formatter =
new DecimalFormat ( "0.00" );
System.out.println("Enter desired accuracy of e: ") ;
accuracy=keyboard.nextDouble();
//calculate estimation
while (number < accuracy )
{
factorial *= number;
e += 1.0 / factorial;
number++;
}
System.out.print ("e is " +Formatter.format (e));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.