Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Big e Name: Bige.java is known as Euler\'s number and is another magic number li

ID: 3726106 • Letter: B

Question

Big e Name: Bige.java is known as Euler's number and is another magic number like . The numerical value of is approximately 2.71828. The following formula can produce an estimate of e: e = 1/1! + 1/2! + 1/3! + 1/4!.... 1/i!

  

Write a program in java that gets a number from the user for how many iterations and how many decimal places and displays the resulting estimate of . Note that for large amounts of iterations and decimal places, your program could take a long time to run.

Sample Runs:

Enter number of iterations: 100

Enter number of decimal places: 5

Estimate: 2.71921

Explanation / Answer

import java.util.*;

class Bige

{

    public static int fact(int n)

    {

        int i, ans = 1;

       

        for( i = 1 ; i<= n ; i++ )

            ans *= i;

       

        return ans;

    }

   

    public static void main(String[] args)

    {

        // create a Scanner type object

        Scanner sc = new Scanner(System.in);

       

        System.out.print("Enter number of iterations: ");

        int n = sc.nextInt();

       

        System.out.print(" Enter number of decimal places: ");

        int d = sc.nextInt();

       

        double e = 0.0;

       

        int i;

       

        for( i = 1 ; i <= n ; i++ )

            e += 1.0 / (double)fact(i);

   

        System.out.printf(" Estimate : %.5f", e);

    }

}