Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java write a program of the following: The value e^x can be approximated by t

ID: 3639394 • Letter: I

Question

In java write a program of the following:
The value e^x can be approximated by the sum: 1+x+((x^2)/2!)+((x^3)/3!)+...+((x^n)/n!)
Write a program that takes a value of x as input and outputs the sum for n taken to be each of the values 1 to 10, 50, and 100. Your program should repeat the calculation for new values of x until the user says she or he is through. The expression n! is called the factorial of n and is defined as n! = 1*2*3*...*n
Use variables of type double to store the factorials (or arrange your calculations to avoid any direct calculation of factorials); otherwise, you are likely to product integer overflow, that is, integers larger than Java allows.

Please be descriptive of how you wrote this.

Explanation / Answer

package geometricSeries;

import java.util.Scanner;

public class GeometricSeries {
   private static Scanner scanner = new Scanner(System.in);
  
   public static void main(String[] args) {
       System.out.print("Enter x value: ");
       double x = scanner.nextDouble();
      
       //n = 1 to 10
       for (int n = 1; n <= 10; n++) {
           System.out.format("n = %3d: e^%f = %f ", n, x, eX(x, n));
       }
       //n = 50
       System.out.format("n = %3d: e^%f = %f ", 50, x, eX(x, 50));
       //n = 100
       System.out.format("n = %3d: e^%f = %f ", 100, x, eX(x, 100));

   }
  
   private static double eX(double x, int n) {
       double sum = 1.0; //first term
       for (int i = 1; i <= n; i++) {
           int k = i;
           double ithTerm = 1;    //ithTerm = x^i / i!
           while (k > 0) {
               ithTerm *= x / k--;   //x/k * x/(k-1) * x/(k-2) * .... * x/2 * x/1 (starting k = i, then k keeps decrementing until k == 0)
           }
           sum += ithTerm; //add ithTerm to the sum
       }
       return sum;
   }
}


Sample run:
Enter x value: 7.5
n =   1: e^7.500000 = 8.500000
n =   2: e^7.500000 = 36.625000
n =   3: e^7.500000 = 106.937500
n =   4: e^7.500000 = 238.773438
n =   5: e^7.500000 = 436.527344
n =   6: e^7.500000 = 683.719727
n =   7: e^7.500000 = 948.568708
n =   8: e^7.500000 = 1196.864628
n =   9: e^7.500000 = 1403.777895
n = 10: e^7.500000 = 1558.962845
n = 50: e^7.500000 = 1808.042414
n = 100: e^7.500000 = 1808.042414

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote