Factorial of n recursive and iterative

Recursive solution:

public int Factorial ( int n ) {
	if ( n == 0 ) {
		return 1;
	} else {
		return n * Factorial(n-1);
	}
}

Iterative Solution:

public int factorial ( int input ) {
	int x, fact = 1;
	for ( x = input; x > 1; x--) {
		fact *= x;
	}
	return fact;
}

Leave a Reply

You must be logged in to post a comment.