Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Christian
- {
- public static void main(String[] args)
- {
- Scanner input = new Scanner(System.in);
- System.out.print("Enter a number x to compute e^x: ");
- int x = input.nextInt();
- System.out.print("Enter a Precision: ");
- double precision = input.nextDouble();
- double sum = 0, result = 0, difference = 0, n = 0;
- do
- {
- double numerator = Math.pow(x, n);
- double denominator = factorial(n);
- result = numerator/denominator;
- sum += result;
- difference = sum - result;
- ++n;
- }while(difference > precision );
- System.out.print(result);
- }
- public static double factorial(double n)
- {
- int ret = 1;
- for (double i = 1.0; i <= n; ++i) ret *= i;
- return ret;
- }
- }
Add Comment
Please, Sign In to add comment