User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,360 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,029 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 14393 | Replies: 5
Reply
Join Date: Aug 2004
Posts: 3
Reputation: TimGerber is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
TimGerber TimGerber is offline Offline
Newbie Poster

Factorial program

  #1  
Aug 31st, 2004
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2004
Posts: 3
Reputation: TimGerber is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
TimGerber TimGerber is offline Offline
Newbie Poster

Re: Factorial program

  #2  
Aug 31st, 2004
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
----------------------------------------------------------
 
public class Test22
{
  public static void main(String[] args) 
  {
    int prod = 1;
    long limit = 10;        // Calculate factorial of integers up to this value
    long factorial = 1;     // Calculate factorial in this variable
     
      System.out.println( "      " + "Number     Factorial" );      // prints titles for columns
      System.out.println( "     " + "========   ===========" );    // prints underlining of titles

    // Loop from 1 to the value of limit
    for(int i = 1; i <= limit; i++)
    {
      factorial = 1;       // Initialize factorial
      int j =2;
      while(j <= i)
        factorial *= j++;
      
      prod = prod * j;
     
      System.out.println( "       " + i + "            " + factorial);

      prodsum = prodsum + prod;
      rowsum = rowsum + i;
     
    }
      
      System.out.println( "   " + "=========================" ); 
      System.out.println( "      " + rowsum + "            " + prodsum );

  }
}
Reply With Quote  
Join Date: Aug 2004
Posts: 3
Reputation: TimGerber is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
TimGerber TimGerber is offline Offline
Newbie Poster

Re: Factorial program

  #3  
Aug 31st, 2004
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
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: Factorial program

  #4  
Sep 1st, 2004
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
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Re: Factorial program

  #5  
Sep 1st, 2004
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.

/* Section A: Variables Inialization */
int factorialFor_i = 1;
int termSum = 0;
int factorialSum = 0;

System.out.println("Number\tFactorial");

int LIMIT = 10;
/* Loop from i =  1 to LIMIT (10) */
for (int i = 1; i <= LIMIT; i++) {

	/* Section B: Loop */
	
	factorialFor_i *= i; 		// B1: update current factorial number
	
	termSum += i; 			// B2: accumulate the sum for the current term i
	factorialSum += factorialFor_i; // B3: accumulate the sum for factorials
	
	System.out.println(i + "\t" + factorialFor_i);
}

/* Section C: Done Loop */
System.out.println("Totals:");
System.out.println(termSum + "\t" + factorialSum);
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Join Date: Aug 2004
Location: Hanover
Posts: 152
Reputation: cosi is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
cosi's Avatar
cosi cosi is offline Offline
Junior Poster

Solution Re: Factorial program

  #6  
Sep 1st, 2004
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:
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:
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:
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
In a world without walls or fences,
What use are Windows and Gates.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 12:13 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC