qq_40940514
小~~
2017-11-22 12:55

计算1到N的阶乘之和,结果是乱码,请大神帮忙看看

  • c

#include
unsigned long Fact(unsigned int n );
int main()
{

int m;
unsigned long  sum;
long ret;
do{
    printf("input m:");
    scanf("%d",&m);
}while (m<0);    /*增加对输入数据的限制,确保输入的数据为无符号整型*/
sum=Fact(m);
printf("sum=%lu\n",sum);

}
/*函数功能:用迭代法计算无符号整型变量n!,再求和*/
unsigned long Fact(unsigned int n )
{
unsigned int i;
unsigned long result =1,sum;
for (i=1;i<=n;i++)
{
result *=i;
sum+=result;
}

return sum;

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答