OpenStudy (anonymous):

Computation Java question! Write a program that computes the value of + 1.0/(1!) + 1.0/(2!) + 1.0/(3!) + .... + 1.0/(n!) Your program should prompt for and read from the keyboard the value of n. Compute each term from the previous term. Also include the following statement in your program: System.out.println("e = " + Math.E); Use type double for your computations. Run your program for n = 1, 10, 20, 50, and 100.

3 years ago
OpenStudy (anonymous):

This is what I came up with so far, I need some help. import java.util.Scanner; class C4h4 { public static void main(String[] args) { int count = 1, n, k; double sum = 0, computation; Scanner kb = new Scanner(System.in); n = kb.nextInt(); k = n - 1; computation = 1.0/(n * k); while (count <= n) { computation = 1.0/(n * k); while (k >= 1) { k--; } sum = sum + computation; count++; } System.out.println("e = " + Math.E); } }

3 years ago
OpenStudy (anonymous):

What's going on with it? Are you getting an error? Is it giving the wrong output? infinite loop?

3 years ago
OpenStudy (kainui):

Can you explain your reasoning? By that I mean, insert comments into your code. I'll add some comments to your code to show where I don't follow what's going on. ``` import java.util.Scanner; class C4h4 { public static void main(String[] args) { //What is this int count = 1, n, k; line do? //Did you mean: //int count = 1; //int n = 0; //int k = 0; int count = 1, n, k; double sum = 0, computation; Scanner kb = new Scanner(System.in); n = kb.nextInt(); k = n - 1; computation = 1.0 / (n * k); while (count <= n) { computation = 1.0 / (n * k); while (k >= 1) { k--; } sum = sum + computation; count++; } System.out.println("e = " + Math.E); } } ``` Also, if you want to type code boxes like this, just use this symbol: ` use a line of 3 of them before and after a bunch of code for this effect: ``` code! awesome ``` or you can just use one on either side of code `like this` and you can make code in your text.

3 years ago
OpenStudy (kainui):

The way I would suggest tackling this problem is by making two methods, one to calculate factorials and another to iterate through the approximation of e that you enter, which will then call the factorial method. Then print both lines one after the other: ``` System.out.println("approx e = " yourVariableHere); system.out.println("e = " Math.E); ``` That way you're comparing your result to the constant that Java has saved.

3 years ago