Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

need a little help with recursion

807600Sep 17 2007 — edited Sep 20 2007
i don't follow this recursion demo very well. After 5 is passed to the n parameter of factR(), could anyone give me an explanation of how it works?
Thanks.
class Factorial {

	int factR(int n) {

	int result;

	if(n == 1) return 1;

	result = factR(n-1)*n;

	System.out.println("result "+result);  //2,6,24,120
	
	return result;

	}

}

class RecursionDemo {

	public static void main(String[] args) {

	Factorial f = new Factorial();

	System.out.println("Factorial of 5 is "+f.factR(5));

	}

}
result 2
result 6
result 24
result 120
Factorial of 5 is 120
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 18 2007
Added on Sep 17 2007
38 comments
319 views