//求n的阶乘
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n = 0;
int sum = 1;
scanf( "%d", &n);
while (n) //n如果为零,则不进入循环体,输出的sum初始值就是1
{
sum *=n;
n--;
}
printf( "%d\n", sum);
system( "pause");
return 0;
}
//求从1到n的阶乘和
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n = 0;
int ret = 1;
int sum = 0;
scanf( "%d", &n);
for (int i = 1; i <=n;i++)
{
ret *= i; //ret是一直没有释放,从1的阶乘变到n的阶乘
sum += ret;
}
printf( "%d\n",sum);
system( "pause");
return 0;
}
本文出自 “11132019” 博客,请务必保留此出处http://11142019.blog.51cto.com/11132019/1753287
原文地址:http://11142019.blog.51cto.com/11132019/1753287