JavaMath Solutions

Method that recursively calculates the factorial value

The following Java coding solution is the first in a series of solutions for common recursion programming problems. It is perhaps not the best solution, and certainly not the only solution. If you’ve got a better idea, please share.

Problem: Write a method called fact that recursively calculates the factorial value of its single int parameter. The value returned by fact is a long.

public long fact(int n) {
	if(n<=1) {
		return 1;
	} else {
		return (fact(n-1)*(long)n);
	}
}

Leave a Reply