Not a good idea. This would prevent the system from crashing, but would hide some other serious problem with the input data.
Here is a complete program that uses the factorial()
//FactorialTester.java // class FactorialCalc { int factorial( int N ) { if ( N == 0 ) return 1; else return n * factorial( N-1 ) ; } } 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?