【发布时间】:2015-01-08 16:51:49
【问题描述】:
我刚刚开始学习 C 编程,并认为我会从一个非常基本的问题开始,即计算一个数字的阶乘。我的代码输出正确的值直到 13 的阶乘,然后在输入大于 13 时给我错误的答案。我的代码是:
#include<stdio.h>
long int factorial(int);
int main()
{
int num;
long int fact;
printf("Please type the number you want factoralized: ");
scanf("%d",&num);
fact = factorial(num);
printf("%d",fact);
return 0;
}
long int factorial(int dig)
{
long int facto;
if (dig>1)
facto = dig * factorial(dig-1);
else if (dig=1)
facto = 1;
return facto;
}
当我输入 13 时,它返回 1932053504 而不是预期的 6227020800
【问题讨论】: