Ask your own question, for FREE!
Computer Science 11 Online
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.

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); } }

OpenStudy (anonymous):

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

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.

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.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
HeyItsAlicia: Why was questioncove not working??
4 hours ago 2 Replies 1 Medal
Countless7Echos: Ah trying out the whole T.V girl drawing :p (I love drawing eyes)
13 hours ago 14 Replies 6 Medals
kaelynw: starting to draw a hand
2 days ago 17 Replies 2 Medals
Twaylor: Rate it :D (Took 2 days)
6 days ago 7 Replies 0 Medals
XShawtyX: Art, Short Writing Assignment: Imagining Landscapes
2 days ago 9 Replies 1 Medal
XShawtyX: Chemistry, Help ud83dude4fud83cudffe
1 week ago 13 Replies 1 Medal
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!