Recursive factorial in Java


public class Factorial {

	/**
	 * illustrates a recursive factorial
	 */
	public static void main(String[] args) {
		System.out.println(factorial(3));
		System.out.println(factorial(10));

                // you can also compute factorial in a loop
	}
	
        /**
	   Paramters: integer n
           Returns: n! for non-negative n, 1 for negative n
        **/
	public static int factorial(int n) {
		if (n <= 1) return 1;
		return n * factorial(n - 1);
 	}

}

CSci 2101 course web site.