Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Help me (http://www.go4expert.com/forums/help-me-t7598/)

bashamsc 28Nov2007 15:29

Help me
 
I have written a program for factorial in recursive and i am not able get how the program work.
The program is as follows
Code:

#include<iostream.h>
main()
{
int fact(int m);
unsigned long int f;
cout<<"enter the no. "<<endl;
int n;
cin>>n;
f = fact(n);
cout<<"factorial of the no. is  "<<f<<endl;
}
int fact(int m)
{
unsigned long int f=1;
if(m==0|m==1)
return(m);
else
{
cout<<"before m = "<<m<<endl;
f=m*fact(m-1);
cout<<"after m = "<<m<<endl<<"fact(m-1) =  "<<fact(m-1)<<endl;
cout<<"f = "<<f<<endl;
return(f);
}
}

I am getting out put like this. Can any one explain me why i am getting like this
enter the no.
3
before m = 3
before m = 2
after m = 2
fact(m-1) = 1
f = 2
before m = 2
after m = 2
fact(m-1) = 1
f = 2
after m = 3
fact(m-1) = 2
f = 6
factorial of the no. is 6

shabbir 28Nov2007 17:23

Re: Help me
 
if(m==0|m==1) should be if(m==0||m==1) see the sign of OR its ||


All times are GMT +5.5. The time now is 06:53.