《C程序设计第二版》(谭浩强)
[相关] [033] 阶乘->数据的范围
n!的递归公式:
┌ 1 (n = 0, 1)
n!=ㄧ
└ n * (n - 1)! (n > 1)
#include <stdio.h>
float fac(int n)
{
float f;
if(n < 0)
printf("n < 0, error!");
else
if(n == 0 || n == 1)
f = 1;
else
f = fac(n - 1) * n;
return f;
}
int main(void)
{
int n;
float y;
printf("input a integer:");
scanf("%d", &n);
y = fac(n);
printf("%d!=%-20.0f\n", n, y);
return 0;
}
运行结果(VC):
==========================
input a integer:20↙
20!=2432902023163674600
==========================
评论