# Factorial Calculation for integers 1-5

Page 1 of 1

## 13 Replies - 13109 Views - Last Post: 03 March 2009 - 08:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=90426&amp;s=ce34c32243e26d89cc0c1cf496338fab&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rekijitsu89

Reputation: 0
• Posts: 9
• Joined: 23-April 08

# Factorial Calculation for integers 1-5

Posted 03 March 2009 - 06:57 PM

```public class FindFactorial
{
public static void main (String args[])
{
int value = 1;
System.out.printf("Value   Factorial\n");
while (value <= 5)
{
switch (value)
{
case 1:
{
System.out.printf("1:	  %d\n", 1);
value++; continue;
}
case 2:
{
System.out.printf("2:	  %d\n", 2*1);
value++; continue;
}
case 3:
{
System.out.printf("3:	  %d\n", 3*2*1);
value++; continue;
}
case 4:
{
System.out.printf("4:	  %d\n", 4*3*2*1);
value++; continue;
}
case 5:
{
System.out.printf("5:	  %d\n", 5*4*3*2*1);
value++; continue;
}
}
}
}
}

/* The obvious problem with evaluating something like 20 is two things, for one, it is in tabular format,
which in the simple manner I did my table in would very likely lose formatting and the larger numbers like
the factorial of 20 would move the number further to the right, and from an appearance perspective wouldn't look
very clean, even if it *appears* like I made it left justified for any value that is single digit. The second and
more noticable problem is that this program is not very flexible. It does what the example asks it to do, but
with my switch statement to evaluate each integer, I would also have to change the value of the loop control
variable "value" to 20, if I kept consistant and used value++, I would have to do 6-19 as well in the display.
Also, I have to edit the source code in my program to do anything like 20, it isn't input by the user,
but the example didn't ask for it to be. */
```

This is what I have as my program, basically the assignment reads as such:
"Write an app that evaluates the factorials of the integers from 1-5. Display the results in tabular format. What difficulty might prevent you from calculating the factorial of 20?"

Our teacher normally doesn't take off for flexibility... but as you can see I didn't even really use an algorithm here, I basically calculated the value through each case... I hate to leave it so inflexible, but should I Keep it Simple Stupid because the book says to only evaluate 1-5?

Is This A Good Question/Topic? 0

## Replies To: Factorial Calculation for integers 1-5

### #2 rekijitsu89

Reputation: 0
• Posts: 9
• Joined: 23-April 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 07:09 PM

I know this sounds completely noobish but...

```public class FindFactorial
{
public static void main (String args[])
{
int value = 1;
System.out.printf("Value   Factorial\n");

switch (value)
{
case 1:
{
System.out.printf("1:	  %d\n", 1);
value++;
}
case 2:
{
System.out.printf("2:	  %d\n", 2*1);
value++;
}
case 3:
{
System.out.printf("3:	  %d\n", 3*2*1);
value++;
}
case 4:
{
System.out.printf("4:	  %d\n", 4*3*2*1);
value++;
}
case 5:
{
System.out.printf("5:	  %d\n", 5*4*3*2*1);
value++;
}
}

}
}

/* The obvious problem with evaluating something like 20 is two things, for one, it is in tabular format,
which in the simple manner I did my table in would very likely lose formatting and the larger numbers like
the factorial of 20 would move the number further to the right, and from an appearance perspective wouldn't look
very clean, even if it *appears* like I made it left justified for any value that is single digit. The second and
more noticable problem is that this program is not very flexible. It does what the example asks it to do, but
with my switch statement to evaluate each integer, I would also have to change the value of the loop control
variable "value" to 20, if I kept consistant and used value++, I would have to do 6-19 as well in the display.
Also, I have to edit the source code in my program to do anything like 20, it isn't input by the user,
but the example didn't ask for it to be. */
```

I was able to get rid of the while loop entirely, dunno why I needed it honestly, maybe I incorporated it just because we have been learning about them... lol.

### #3 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 07:19 PM

KISS is a nice idea!

You are working too hard doing it in a straight line approach.

Instead of a while() loop, think how you would calculate a factorial using a for() loop. For each case in your switch statement, you are doing by hand (so to speak) what a for() loop could be doing for you.

As for the difficulty question, just how big is 20!?

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 07:48 PM

You are not making a factorial but a sumation

```int x = 10;
int sum = 0;
for(int i = 1; i <= 10; i++)
sum += i;

```

factorial would be

```int x = 10;
int facto = 1;
for(int i = 1; i <= 10; i++)
facto *= i;

```

### #5 rekijitsu89

Reputation: 0
• Posts: 9
• Joined: 23-April 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:07 PM

How am I doing a sumation? I didn't take the addition of each but rather I took the product of all values 1 - > x multiplied together. It may be overly simple though...

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:21 PM

rekijitsu89, on 3 Mar, 2009 - 07:07 PM, said:

How am I doing a sumation? I didn't take the addition of each but rather I took the product of all values 1 - > x multiplied together. It may be overly simple though...

value++; looks like an addition to me

### #7 rekijitsu89

Reputation: 0
• Posts: 9
• Joined: 23-April 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:26 PM

Yeah value actually isn't the value of a factorial for me, in fact I have no variable to account for the factorial value of a number. The value is more or less for each case so that my program continues to evaluate the factorial of the next case (1, 2, 3 +), and running the multiplication within each print statement. It isn't fancy at all really, and not very flexible since it just does simple math in the print statement itself.

### #8 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:31 PM

pbl, on 3 Mar, 2009 - 09:21 PM, said:

rekijitsu89, on 3 Mar, 2009 - 07:07 PM, said:

How am I doing a sumation? I didn't take the addition of each but rather I took the product of all values 1 - > x multiplied together. It may be overly simple though...

value++; looks like an addition to me

Look in the printf(...) statements. The calculation(s) are there.

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:34 PM

Locke, on 3 Mar, 2009 - 07:31 PM, said:

pbl, on 3 Mar, 2009 - 09:21 PM, said:

rekijitsu89, on 3 Mar, 2009 - 07:07 PM, said:

How am I doing a sumation? I didn't take the addition of each but rather I took the product of all values 1 - > x multiplied together. It may be overly simple though...

value++; looks like an addition to me

Look in the printf(...) statements. The calculation(s) are there.

Locke: 1 PBL: 0
So what is value used for ?

### #10 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:35 PM

pbl, on 3 Mar, 2009 - 09:34 PM, said:

Locke: 1 PBL: 0

pbl, on 3 Mar, 2009 - 09:34 PM, said:

So what is value used for ?

No idea. Right now...useless computation.

This post has been edited by Locke: 03 March 2009 - 08:36 PM

### #11 n8wxs

• --... ...-- -.. . -. ---.. .-- -..- ...

Reputation: 972
• Posts: 3,878
• Joined: 07-January 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:36 PM

Locke, on 3 Mar, 2009 - 07:35 PM, said:

pbl, on 3 Mar, 2009 - 09:34 PM, said:

Locke: 1 PBL: 0

pbl, on 3 Mar, 2009 - 09:34 PM, said:

So what is value used for ?

No idea. Right now...useless computation.

It originally was the loop counter.

### #12 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:39 PM

n8wxs, on 3 Mar, 2009 - 09:36 PM, said:

It originally was the loop counter.

n8wxs: 1
Locke: 1
pbl: 0

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:42 PM

n8wxs, on 3 Mar, 2009 - 07:36 PM, said:

It originally was the loop counter.

So why removing to loop ? Because it looked too much like the code I wrote ? Which will work for any number as the first request was.

### #14 Locke

• Sarcasm Extraordinaire!

Reputation: 550
• Posts: 5,624
• Joined: 20-March 08

## Re: Factorial Calculation for integers 1-5

Posted 03 March 2009 - 08:44 PM

pbl, on 3 Mar, 2009 - 09:42 PM, said:

So why removing to loop ? Because it looked too much like the code I wrote ? Which will work for any number as the first request was.

I don't know...the standard approach to this problem would be a loop...at least, in everyone I know's minds.