by manojthesingham » Tue Sep 01, 2009 6:05 am
/*finding factorial recursively*/
#include<stdio.h>
#include<conio.h>
int main(int argc,char**argv)
{
int x;
printf("Enter the Number to find its Factorial : ");
scanf("%d",&x);
printf("%d",factorial(x));
getch();
return 0;
}
/*recursive factorial method*/
/*note : int function(int a,int b,......,int z) does not need function prototype*/
int factorial (int x)
{
if(x<=1)
return 1;
return x*factorial(x-1);
}
/* how it works :- */
/* tracing factorial(4)*/
/*STACK operation*/
/* --------------------------*/
/* | 2 * factorial(1)=2*1 |-------------->TOP of STACK(Activation Record)*/
/* --------------------------*/
/* | 3 * factorial(2)=3*2 |*/
/* --------------------------*/
/* | 4 * factorial(3)=4*6 |*/
/* --------------------------*/