Computing the Exponential "e" in Java

Problem:

You can computer e using the following Taylor series. Write a program that displays the  e value for  i 10000, 20000, and ... , 100000. Initialize e and item to be 1 and keep adding a new item to e.


Solution:

public class {

  public static double calFactorial(int n)
  {
    double out=1;
    for (int i =n;i>=1;i--)
    {
      out*=i;
    }
    return out;
  }
 public static void main (String[] args)
  {
    double e =1;
    for (int i =10000;i<=100000;i+=10000)
    {
      for (int j =1;j<=i;j++)
      {
        e+= 1.0/calFactorial(j);
      }
      System.out.println("For i = " + i + ", " +
          "e has a value of " + e);
      e=1;
    }
  }
}

No comments:

Post a Comment