import java.util.Scanner;

/* Approximate e^x using series 1 + x/1! + (x^2)/2! + ...
 */
public class Eeee {

  public static void main(String[] args) {
    
    Scanner keyboard= new Scanner(System.in);
    
    System.out.print("Enter power of e: ");
    double x= keyboard.nextDouble();
    double ans= Math.exp(x);  // true value of e^x
    double ex= 1;             // approx value of e^x so far
    double tol= 0.0001;       // error tolerance
    int kfact;                // k!
    int i, k;
    
    k= 1;
    while (Math.abs(ans-ex) > tol) {
      // k!
      kfact= 1;
      i= 2;
      while ( i<=k ) {
        kfact *= i;
        i++;
      }
      
      // add new term
      ex= ex + Math.pow(x,k)/kfact;
      
      // update counter
      k++;
    }
    
    System.out.print("\nError after " + k);
    System.out.println(" terms:  " + Math.abs(ans-ex));
  }
}
