Code:
#include<cstdlib>
#include<iostream>
using namespace std;
int factorial(int *,int); //number and result
int main()
{
int fact,num;
cout<<"Enter a number!"<<endl;
cin>>num;
fact=factorial(&num,1);
cout<<"fact is : "<<fact<<endl;
system("PAUSE");
return 0;
}
int factorial(int *n,int r)
{
if(*n<=1)
return r;
else
{
r = r * (*n);
*n-=1;
cout<<"r is : "<<r<<endl;
factorial(n,r);
}
}
This is a factorial program everything works fine, but the output of fact in main is way off and the variable returning to fact in function factorial is giving the right output.
This is the second time this happened, so any ideas?
I'm using Dev-cpp, just for the record!
Thank You!