I am trying to learn recursion. for the starting problem of it , which is calculating factorial of a number I have accomplished it using two methods. the first one being the normal usual approach. the second one i have tried to do something different. in the second one i return the value of n at the end rather than getting the starting value as in the first one which uses backtracking. my question is that does my approach has any advantages over backtracking? if asked to chose which one would be a better solution?
//first one is ,
ll factorial(int n)
{
if(n==1)
return 1 ;
return n*factorial(n-1) ;
}
int main()
{
factorial(25) ;
return 0 ;
}
// second one is ,
ll fact(ll i,ll n)
{
if(i==0)return n ;
n=(n*i)
i--;
n=fact(i,n);
}
int main()
{
int n ;
cin>>n ;
cout<<fact(n,1) ;
return 0 ;
}
// ll is long long int