Not a good idea. This would prevent the system from crashing, but would hide some other serious problem with the input data.
// FactorialTester.java
//
class FactorialCalc
{
public int factorial( int N )
{
if ( N == 0 )
return 1;
else
return N * factorial( N-1 ) ;
}
}
public class FactorialTester
{
public static void main ( String[] args)
{
int argument = 10;
FactorialCalc f = new FactorialCalc();
int result = f.factorial( argument );
System.out.println("Factorial(" + argument + ") is " + result );
}
}
Here is a run of the program:
C:\JavaNotes\Recursion03>javac FactorialTester.java C:\JavaNotes\Recursion03>java FactorialTester Factorial(10) is 3628800
Here is another run of the program with a new value for the argument:
C:\JavaNotes\Recursion03>java FactorialTester Factorial(17) is -288522240
That answer does not look correct. What went wrong?