Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (ajprincess):

Pls help:) Write a program called Product1ToN to compute the product of integers 1 to 10 (i.e., 1×2×3×...×10). Try computing the product from 1 to 11, 1 to 12, 1 to 13 and 1 to 14. Write down the product obtained and explain the results

OpenStudy (ajprincess):

I am stuck with only the explanation part.

OpenStudy (ajprincess):

@TuringTest

OpenStudy (ajprincess):

@experimentX

OpenStudy (turingtest):

where are you stuck? have you written any pseudocode? in what language are you working?

OpenStudy (turingtest):

you say you are stuck only with the explanation...?

OpenStudy (ajprincess):

Java. ya. here is the code. public class Product1toN { public static void main(String[] args) { int N=10; int product=1; for(int i=1;i<=N;i++) { product*=i; } System.out.print("Product of numbers from 1 to "+N+" is "+product); } }

OpenStudy (turingtest):

I'd say that depends on how you wrote your code

OpenStudy (turingtest):

well it's poorly-worded, to be sure. "explain the results" could mean a lot of things. I guess i'd describe how your loop works in words.

OpenStudy (ajprincess):

Oh k.I thought we need to explain about the factorial.

OpenStudy (turingtest):

that;s why I said it's poorly-worded... I don't know if you are translating this from another language or what, but if this is direct from the tectbook, that's lame. The literal "result" is that the series n! for 0<n<11 is printed to standard out, which is almost cerstainly your screen, in the format string you wrote in your system.out.print statement. I don't know java syntax at all btw, I can only gather from c++

OpenStudy (experimentx):

haven't tested public class Test{ public static void main(String[] args){ System.out.println(fact(10)); } public static int fact(int n){ if(n>1) return fact(n-1)*n; else return 1 } }

OpenStudy (experimentx):

*Test.fact(10)

OpenStudy (experimentx):

there you go .. hate Java

OpenStudy (turingtest):

My "explanation of the results" is that each iteration multiplies product, increments i, and sends the resulting print statement to standard out. I can't testify as to the correctness of your code as, again, I don't know java.

OpenStudy (ajprincess):

Actually I got this question from this website. http://www.ntu.edu.sg/home/ehchua/programming/java/J2a_BasicsExercises.html. I got the correct output when I executed my code. thank u so much for both of u for helping me out.:)

OpenStudy (experimentx):

without recursion public class Test{ public static void main(String[] args){ System.out.println(Test.fact(2)); System.out.println(Test.fact(3)); System.out.println(Test.fact(4)); } public static int fact(int n){ int j=1; for(int i=1; i<=n; i++) j = j*i; return j; } }

OpenStudy (experimentx):

how do you put code in OS?

OpenStudy (turingtest):

I'd like to know that as well

OpenStudy (experimentx):

`code`

OpenStudy (experimentx):

`code code code code` multiline does not work :(

OpenStudy (ajprincess):

Latex is nt working for me. is t working for u all?

OpenStudy (experimentx):

latex wasn't working from Windows 8 ... but i logged in using linux ... so it's fine.

OpenStudy (ajprincess):

oh k.:)

OpenStudy (hba):

Is this question answered?

OpenStudy (hba):

Aj princess code generates numbers between 1 and 10.

OpenStudy (hba):

Like in place of 10 you can say n and take user input so then you can ask it to go till 11,12,13,14.

OpenStudy (turingtest):

yeah I don't know java, but that print statement looks like it's in the wrong place. The question though, was about the "explanation of the results", so....?

OpenStudy (hba):

Lol this is just basic java.If you knew C# or C language you could understand.

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!
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!