Last active
November 16, 2016 01:49
-
-
Save JeonghunLee/67b2a51924fa1d614359e8808e953a1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// general recursion | |
int factorial(int n) | |
{ | |
if(n==0) | |
return 1; | |
else | |
return n * factorial(n-1); | |
} | |
//tail recursion | |
int tailfactorial(int n, int pren) | |
{ | |
if(n==1){ | |
return pren; //sum | |
}else{ | |
return tailfactorial(n-1, n*pren); | |
} | |
} | |
//n repeat | |
void reatfactorial(int n, int *sum) | |
{ | |
int i=0 ; | |
for(i=1; i <= n;i++) | |
*sum = *sum * i; | |
} | |
int main(void) { | |
int ret,sum=1; | |
reatfactorial(4,&sum); | |
printf("sum0= %d\n",sum); | |
ret = factorial(4); | |
printf("sum1= %d\n",ret); | |
ret = tailfactorial(4,1); | |
printf("sum2= %d\n",ret); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment