A recursive function which computes a factorial

public class FactTest {
    public static void main (String [] args) {
	int n = Test.fact(5);
	System.out.println(n);
    }
}

class Test {
    // a recursive function fact 
    public static int fact(int n) {
	// base case:
	if (n == 1) return 1;
	return n * fact(n - 1);
    }
}

This is an example from CSci 1211 course.

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.