利用求阶乘函数Fact(),编程计算并输出1! + 2! + …+ n!的值。 **输入格式要求:"%u" 提示信息:"Input n(n>0):" **输出格式要求:"sum = %lu\n"C语言编程
时间: 2024-02-11 12:09:23 浏览: 80
C语言求n的阶乘(n!).txt
以下是C语言的代码:
```c
#include <stdio.h>
unsigned long Fact(unsigned int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * Fact(n - 1);
}
}
int main() {
unsigned int n;
printf("Input n(n>0):");
scanf("%u", &n);
unsigned long sum = 0;
for (unsigned int i = 1; i <= n; i++) {
sum += Fact(i);
}
printf("sum = %lu\n", sum);
return 0;
}
```
运行结果示例:
```
Input n(n>0):5
sum = 153
```
阅读全文