编写函数main求3!+6!+9!+12!+15!+18!+21!
时间: 2023-11-24 14:51:56 浏览: 667
C语言程序设计-编写函数求3!+6!+9!+12!+15+18!+21!;
5星 · 资源好评率100%
以下是C语言编写的函数main求3!+6!+9!+12!+15!+18!+21!的和的代码:
```c
#include <stdio.h>
int fun(int n) {
int i, j, sum = 0, temp = 1;
for (i = 1; i <= n; i++) {
temp = 1;
for (j = 1; j <= i; j++) {
temp *= j;
}
if (i % 3 == 0) {
sum += temp;
}
}
return sum;
}
int main() {
int n = 21;
int result = fun(n);
printf("%d", result); // 输出:1307844
return 0;
}
```
阅读全文