Guest User

Untitled

a guest
Jan 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  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. }
Add Comment
Please, Sign In to add comment