0

e can be approximated using the formula e = 1 + (1/1!) + (1/2!) + (1/3!)... + (1/n!). I am trying to use for loops to accept whatever integer the user sets n to be. The program should approximate e by using the formula above from (1/1!) + .... (1/n!) and out put the result.

The nested for loop calculates the factorial of n (tested it separately and it works) and the variable defined, frac, puts the factorial into a fraction of 1/(answer to factorial). I store the value into a variable e and it should add the new fraction to the old value every time an iteration is done. I cannot not understand what is wrong with my loops that they are not out putting the right answer.

System.out.println("Enter an integer to show the result of 
approximating e using n number of terms.");
int n=scan.nextInt();
double e=1;
double result=1;
for(int i=1; n>=i; n=(n-1))
{
    for(int l=1; l<=n; l++)
            {
                result=result*l;
            }
    double frac=(1/result);
    e=e+frac;
}
System.out.println(e);

Output when I enter the integer 7 as n = 1.0001986906956286

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Zach_P
  • 15
  • 1
  • 4
  • 2
    This is not javascript – Sterling Archer Mar 12 '18 at 18:17
  • 1
    That is *not* how you calculate a factorial. When i = 3, "result" ends up as 1 * 1 * 2 * 1 * 2 * 3 = 12 because you don't reset to 1 after each iteration of i. Refer [this question](https://stackoverflow.com/questions/891031/is-there-a-method-that-calculates-a-factorial-in-java). – Roddy of the Frozen Peas Mar 12 '18 at 18:20

1 Answers1

0

You don't need that whole inner loop. All you need is result *= i.

for (int i = 1; i <= n; i++)
{
    result *= i;
    double frac = (1 / result);
    e += frac;
}

Here's a JavaScript version I just threw together:

function init() {
  const elem = document.getElementById('eapprox');

  let eapprox = 1;
  const n = 15;
  let frac = 1;

  for (var i = 1; i <= n; ++i) {
    frac /= i;
    eapprox += frac;
  }

  elem.textContent = eapprox;
}

This yields 2.718281828458995. Plunker here: http://plnkr.co/edit/OgXbr36dKce21urHH1Ge?p=preview

kshetline
  • 12,547
  • 4
  • 37
  • 73
  • Yes, Java (and JavaScript too, and C, and a bunch of languages) have shorthand operators +=, -=, *=, /= for adding, subtracting, multiplying and dividing a variable and putting the result back into that variable. – kshetline Mar 12 '18 at 18:43
  • After trying to understand exactly what your java code is doing I am a little confused. For the first answer you gave me I am using n as whatever input the user types, so if they input n to be 7 (the code should output e = 1 + (1/7!) + (1/6!) + ... (1/1!), how is the for loop stopping if you are counting up with n? Wouldn't i(1) always be less than or equal to n(7) since n is counting up by 1 each iteration? Im not understanding why this is not an endless loop. I really appreciate the help with this, have never coded in any language before this semester! – Zach_P Mar 12 '18 at 19:17
  • The reason it doesn't make sense is I made a typo :) You're right, it would keep going forever. I fixed the typo without even realizing when I did the JavaScript version. – kshetline Mar 12 '18 at 19:52