
Patrick B. answered 04/04/21
Math and computer tutor/teacher
import java.util.Scanner;
class JeffC
{
private Scanner scanner;
public static final int MAX_FACTORIAL = 20;
JeffC()
{
scanner = new Scanner(System.in);
}
public long factorial( int n)
{
long longFactorialReturn = -1;
if (n>=0)
{
if ((n==0) || (n==1))
{
longFactorialReturn = 1;
}
else
{
longFactorialReturn=n * factorial(n-1);
}
}
return(longFactorialReturn);
}
public long[] Factorials ( int n)
{
long factorialArrayReturn[] = new long[n+2]; //0!, 1!,2!, .... (n-1)!, n! is (n+1) long values
for (int iLoop=0; iLoop<=n; iLoop++)
{
factorialArrayReturn[iLoop]=factorial(iLoop);
}
return(factorialArrayReturn);
}
public void Go()
{
int nInput = -1;
while ((nInput<0) || (nInput>MAX_FACTORIAL))
{
System.out.print("Please input the integer value :>");
nInput = scanner.nextInt();
if (nInput<0) { System.out.println("\n Say what? \n"); }
if (nInput>MAX_FACTORIAL) { System.out.println(" \n No, sorry too many "); }
}
long factorials[] = Factorials(nInput);
for (int iLoop=0; iLoop<=nInput; iLoop++)
{
System.out.println( iLoop + "! = " + factorials[iLoop] );
}
}
public static void main(String args[])
{
JeffC x = new JeffC();
x.Go();
}
}