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?

New Topic/Question
Reply




MultiQuote





|