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

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact