Caculate 1!+2!+...+n! Input: An integer for n. Output: An interger for the result.c语言实现
时间: 2023-10-09 09:07:49 浏览: 144
vs2019 实现C#调用c++的dll两种方法
以下是C语言实现:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += factorial(i);
}
printf("The sum of factorials from 1 to %d is %d\n", n, sum);
return 0;
}
```
首先定义了一个计算阶乘的函数`factorial()`,然后在主函数中循环计算1到n的阶乘并求和,最终输出结果。
阅读全文