编写程序求1!+2!+……n!的和。

2025-06-26 07:00:34
推荐回答(1个)
回答1:

#include
#include

typedef unsigned long long ULL;
ULL factorial(int n)
{
if(n == 1)

return 1;

int i ;

ULL f = 1;

for(i = 2; i <= n; i++)

f = f*n;

return f;

}

void main()
{
int s, d;

printf("please input the start number and the end number: ");

scanf("%d%d", &s, &d);

ULL ret = factorial(s);
int i;

for(i = s + 1; i <= d; i++)

ret += ret * i;

printf("The sum of factorial from %d to %d is: %lu\n", s, d, ret);

}
上面的程序,factorial是用来求阶乘,main函数的for循环就可以用来求区间[s, d]之间的阶乘之和。。