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.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.