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