public static void main (String[] args)
{
System.out.println(factorial(5));
}
public int factorial(int n)
{
if(n <= 1){
return 1;
}
else{
return n * factorial(n - 1);
}
}
I wrote the above directly in here so may not compile but think it does.
Can anyone breiefly explain how this works in the sence that how is it stored? It starts off by calculating 5 * (5-1), then down to 4 * (4-1) then 3 * (3-1)..... until it gets to 1 which will just return 1 right? sorry for being so sketchy i would just be interested to find out how this works exactly
thanks
but as it works it out - it gets the values for the individual stages
5*(5-1) 4 * (4-1) ... ... ...
how are these stored and then retrieved back or am i missing something?