C语言--------求1!+ 2!+ 3!+ 4!+ 5!的和
方法一:
#include<stdio.h>
int main()
{
int sum = 0;
int n = 0;
int i = 0;
for (n = 1; n <=5; n++)
{
//求n的阶乘
int ret = 1;//注意:将ret每次置为1
for (i = 1; i <= n; i++)
{
ret *= i;
}
sum += ret;//1!+ 2!+ 3!+ 4!+ 5!
}
printf("%d", sum);
return 0;
}
方法二:
#include<stdio.h>
int main()
{
int sum = 0;
int n = 0;
int i = 0;
int ret = 1;
for (n = 1; n <= 5; n++)
{
//1! = 1
//2! = 2 * 1!
//3! = 3 * 2!
//4! = 4 * 3!
//5! = 5 * 4*
ret *= n;
sum += ret;
}
运行结果: