SHARE
TWEET

Untitled

a guest Jan 20th, 2019 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class Christian
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         Scanner input = new Scanner(System.in);
  9.        
  10.         System.out.print("Enter a number x to compute e^x: ");
  11.         int x = input.nextInt();
  12.         System.out.print("Enter a Precision: ");
  13.         double precision = input.nextDouble();
  14.        
  15.         double sum = 0, result = 0, difference = 0, n = 0;  
  16.         do
  17.         {
  18.             double numerator = Math.pow(x, n);
  19.             double denominator = factorial(n);
  20.            
  21.             result = numerator/denominator;
  22.             sum += result;
  23.             difference = sum - result;
  24.             ++n;
  25.            
  26.         }while(difference > precision );
  27.         System.out.print(result);
  28.     }
  29.    
  30.     public static double factorial(double n)
  31.     {
  32.         int ret = 1;
  33.             for (double i = 1.0; i <= n; ++i) ret *= i;
  34.             return ret;
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top