Input | How factorial works by using recursion |
5 | factorial(5) =5*factorial(5-1) =5*factorial(4) =5*{4*factorial(4-1)} =5*4*factorial(3) =5*4*{3*factorial(3-1)} =5*4*3*factorial(2) =5*4*3*{2*factorial(2-1)} =5*4*3*2*factorial(1) =5*4*3*2*{1*factorial(1-1)} =5*4*3*2*1*factorial(0) =5*4*3*2*1*{1} =5*4*3*2*{1*1} =5*4*3*{2*1} =5*4*{3*2} =5*{4*6} =5*24 =120 |
Source Code:
#include<bits/stdc++.h>
unsigned long long factorial(unsigned long long);
int main()
{
unsigned long long p,result;
while(scanf(“%llu”,&p)==1)
{
result=factorial(p);
printf(“Factorial: %llu\n”,result);
}
return 0;
}
unsigned long long factorial(unsigned long long n)
{
if(n==0)
return 1;
else
return n*factorial(n-1);
}
Very efficiently written information. It will be valuable to anybody who utilizes it, as well as me. Keep doing what you are doing – looking forward to more posts.
F*ckin’ amazing things here. I’m very glad to see your post. Thanks a lot and i’m looking forward to contact you. Will you please drop me a e-mail?