C/Recursion
Expert: Prince M. Premnath - 10/19/2006
QuestionI was going through a "C" program on calculating factorial using recursion,where there was no separate code for calculating factorial i mean in the function it was something like if n>=1 then fact(n)=n*fact(n-1) and in the main the printing was done so just by writing those two statements how can the factorial be calculated?
AnswerDear janakiram !
Clearly i cant under stand your question
if you want code to compute factorial just use the code given below!
unsigned int Factorial (unsigned int n)
{
if (n == 0)
return 1;
else
return n * Factorial (n - 1);
}
You need explanation on this code ?
This procedure uses a unique feature supported in C/C language called recursion ie, A function have the capability call its own image
For each and every call there are different values passed to the function , and simultaneously the results will be stored in the stack maintained by the system !
In order to know completely about the stack , atleast you needs to have some knowledge about the data structure ! if so send in me a personal mail so that i can answer you very clearly about the recursion
if you are a beginner have a little knowledge is enough regarding the recursion.
Regards.
Prince M. Premnath.