/*
The Program for calculating factorial of a no.
Here, Fact is a recursive function
It will calculate factorial as below
if user input is 4
fact(4) // call from main m=4,n=4;
fact(4-1)*4 will call function it self as recursion
fact(3) m=3
fact(3-1)*3
fact(2)
fact(2-1)*2
fact(1) // it will return 1 to last cll of fact
(1 * 2) // 1 comes from upper call's return value
((1*2) *3) = (2*3) // 2 comes from very upper call's return value
( (1*2*3) * 4)
(6*4)=24 // 6 comes from upper last call of fact's return values
and at last 24 will return to main
The program code for showing the recursion mechanism of function.
It will maintain stack call internally for manipulating function calls and
values at diffrent call.
*/
#include "iostream"
#include "conio.h"
using namespace std;
int fact(int m)
{
if(m==1)
return 1;
return fact(m-1)*m;
}
int main(void)
{
int no=5;
cout<<"Enter No to calculate factorial of it : ";
cin>>no;
cout<<"factorial of "<< no <<" is "<< fact(no);
getch();
return 0;
}
Program code for calculating Factorial of a no, How Recursive function will call and showing process of recursion.
Wednesday, April 21, 2010 by Vishal Joshipura |
0
comments
Subscribe to:
Post Comments (Atom)


No comments:
Post a Comment