#include <stdio.h>
//
// 普通の関数
// calc(n) = n * (n-1) * (n-2) * ・・・ * 1
unsigned int calc(unsigned int n){
unsigned int ans = 1;
for ( ; n > 1 ; n-- )
ans*=n;
return ans;
}
//
// 再帰関数
// calc(n) = n * calc(n-1)
unsigned int calc2(unsigned int n){
if ( n <= 1 )
return 1;
return n * calc2(n-1);
}
void main(){
unsigned int n = 5;
printf("%d! = %d\n", n, calc(n));
printf("%d! = %d\n", n, calc2(n));
}
|