//求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;
}
时间: 2024-10-12 22:39:50