944,087 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 47729
  • Java RSS
Aug 31st, 2004
-1

Factorial program

Expand Post »
Hi guys, i need some help with a project, I need to print a table showing the numbers 1 - 10, thentheir factorials, i also need rthe sum of the numbers and the sum of the factorials:\
Number Factorials
=======================
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 322880
10 3628800
=========================
55 4037913

Any sugguestions...i can't use recursive either...thanx
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TimGerber is offline Offline
3 posts
since Aug 2004
Aug 31st, 2004
0

Re: Factorial program

I've can get the factorials....but i can't sum the coulmns...then i got my self confused....can u guys take a look...maybe tell me where i am goin wrong?
thanx
----------------------------------------------------------
Java Syntax (Toggle Plain Text)
  1.  
  2. public class Test22
  3. {
  4. public static void main(String[] args)
  5. {
  6. int prod = 1;
  7. long limit = 10; // Calculate factorial of integers up to this value
  8. long factorial = 1; // Calculate factorial in this variable
  9.  
  10. System.out.println( " " + "Number Factorial" ); // prints titles for columns
  11. System.out.println( " " + "======== ===========" ); // prints underlining of titles
  12.  
  13. // Loop from 1 to the value of limit
  14. for(int i = 1; i <= limit; i++)
  15. {
  16. factorial = 1; // Initialize factorial
  17. int j =2;
  18. while(j <= i)
  19. factorial *= j++;
  20.  
  21. prod = prod * j;
  22.  
  23. System.out.println( " " + i + " " + factorial);
  24.  
  25. prodsum = prodsum + prod;
  26. rowsum = rowsum + i;
  27.  
  28. }
  29.  
  30. System.out.println( " " + "=========================" );
  31. System.out.println( " " + rowsum + " " + prodsum );
  32.  
  33. }
  34. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TimGerber is offline Offline
3 posts
since Aug 2004
Aug 31st, 2004
0

Re: Factorial program

ok i worked out how to use prodsum and so on. but i am confused as to why i am using it? my task is due today but it doesn't matter, i will still get a pass for the work i have already done a a previous task.

Even so...if anyone is goin to reply i would appreicate it as i want to learn why and how prod works...as well as why my loop is wack.
thanx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TimGerber is offline Offline
3 posts
since Aug 2004
Sep 1st, 2004
0

Re: Factorial program

Hey Tim,

Sorry about the homework. You should generally start the homework earlier! ;-)

I can explain your program though I think that it is a bit convoluted. There is a much cleaner and clearer way to code the factorial program you wrote. How about I explain how this way works and see if you understand. If not, then I can explain for you the previous program.

Ed
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Sep 1st, 2004
0

Re: Factorial program

So here is my rendition of the factorial progam you had... we can discuss this program by section number as I have labelled in the comments.

Java Syntax (Toggle Plain Text)
  1. /* Section A: Variables Inialization */
  2. int factorialFor_i = 1;
  3. int termSum = 0;
  4. int factorialSum = 0;
  5.  
  6. System.out.println("Number\tFactorial");
  7.  
  8. int LIMIT = 10;
  9. /* Loop from i = 1 to LIMIT (10) */
  10. for (int i = 1; i <= LIMIT; i++) {
  11.  
  12. /* Section B: Loop */
  13.  
  14. factorialFor_i *= i; // B1: update current factorial number
  15.  
  16. termSum += i; // B2: accumulate the sum for the current term i
  17. factorialSum += factorialFor_i; // B3: accumulate the sum for factorials
  18.  
  19. System.out.println(i + "\t" + factorialFor_i);
  20. }
  21.  
  22. /* Section C: Done Loop */
  23. System.out.println("Totals:");
  24. System.out.println(termSum + "\t" + factorialSum);
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Sep 1st, 2004
0

Re: Factorial program

So let me start with how this program obtains factorials. Let's focus on Section B1:

Remember that this loop's variable i will obtain values from 1 to 10; so the first time the loop executes i = 1, then i = 2, i = 3, and so on.

First Loop Iteration:
Before Loop:
i = 1
factorialFor_i = 1

During Loop:
Java Syntax (Toggle Plain Text)
  1. factorialFor_i *= i; // Multiply factorial by i (1)
so 1*1 = 1

Result:
i = 1, factorialFor_i = 1


Second Loop:
Before Loop:
i = 2
factorialFor_i = 1

During Loop:
Java Syntax (Toggle Plain Text)
  1. factorialFor_i *= i; // Multiply factorial by i (2)
1*2 = 2

Result:
i = 2, factorialFor_i = 2


Third Loop:
Before Loop:
i = 3
factorialFor_i = 2

During Loop:
Java Syntax (Toggle Plain Text)
  1. factorialFor_i *= i; // Multiply factorial by i (2)
2*3 = 6

Result:
i = 3, factorialFor_i = 6

Simple right? Now termSum and factorialSum work the same way.... just step through the loop and see that the values you get are right where you want them. It would be very useful to step through the code in a debugger (if you have been introduced to debuggers, they make life a bit easier by stepping through your code and showing the values for all the variables just like I did here.

I hope this helps! Let me know if you need any more clarification.


Ed
Reputation Points: 17
Solved Threads: 1
Junior Poster
cosi is offline Offline
153 posts
since Aug 2004
Apr 16th, 2010
-1
Re: Factorial program
Java Syntax (Toggle Plain Text)
  1. public class Test22
  2.  
  3. {
  4.  
  5. public static void main(String[] args)
  6.  
  7. {
  8.  
  9. int prod = 1;
  10.  
  11. long limit = 10; // Calculate factorial of integers up to this value
  12.  
  13. long factorial = 1; // Calculate factorial in this variable
  14.  
  15.  
  16.  
  17. System.out.println( " " + "Number Factorial" ); // prints titles for columns
  18.  
  19. System.out.println( " " + "======== ===========" ); // prints underlining of titles
  20.  
  21.  
  22.  
  23. // Loop from 1 to the value of limit
  24.  
  25. for(int i = 1; i <= limit; i++)
  26.  
  27. {
  28.  
  29. factorial = 1; // Initialize factorial
  30.  
  31. int j =2;
  32.  
  33. while(j <= i)
  34.  
  35. factorial *= j++;
  36.  
  37.  
  38.  
  39. prod = prod * j;
  40.  
  41.  
  42.  
  43. System.out.println( " " + i + " " + factorial);
  44.  
  45.  
  46.  
  47. prodsum = prodsum + prod;
  48.  
  49. rowsum = rowsum + i;
  50.  
  51.  
  52.  
  53. }
  54.  
  55.  
  56.  
  57. System.out.println( " " + "=========================" );
  58.  
  59. System.out.println( " " + rowsum + " " + prodsum );
  60.  
  61.  
  62.  
  63. }
  64.  
  65. }
Last edited by peter_budo; Apr 17th, 2010 at 4:17 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mukeshbca2 is offline Offline
1 posts
since Apr 2010
Apr 16th, 2010
0
Re: Factorial program
this is a little confusing. this thread , except for the last two posts by mukeshbca2 is from 2004. Did you find it in a google search?

Mike
Reputation Points: 31
Solved Threads: 27
Posting Whiz in Training
adams161 is offline Offline
279 posts
since May 2008
Apr 17th, 2010
0
Re: Factorial program
@mukeshbca2
  • almost 6 years too late
  • double posted
  • copying code from second post and forgot to ask question
If you have question create new thread as this one is of this moment closed...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: getClass()
Next Thread in Java Forum Timeline: public vs private





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC