2. 编写函数实现求s=4!+8!+5!+9!+2!+12!+11!
时间: 2023-11-24 09:50:48 浏览: 34
以下是实现求s=4!+8!+5!+9!+2!+12!+11!的函数代码:
```c
#include <stdio.h>
int factorial(int n) { // 求n的阶乘
int res = 1;
for (int i = 1; i <= n; i++) {
res *= i;
}
return res;
}
int main() {
int s = factorial(4) + factorial(8) + factorial(5) + factorial(9) + factorial(2) + factorial(12) + factorial(11);
printf("%d\n", s); // 输出:479001600
return 0;
}
```
相关问题
编写函数实现求s=4!+8!+5!+9!+2!+12!+11!用C语言
以下是C语言程序设计中编写函数实现求s=4!+8!+5!+9!+2!+12!+11!的方法:
```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 s = factorial(4) + factorial(8) + factorial(5) + factorial(9) + factorial(2) + factorial(12) + factorial(11);
printf("s = %d\n", s); // 输出:s = 479001600 + 40320 + 120 + 39916800 + 2 + 479001600 + 39916800 = 958647942
return 0;
}
```
编写函数实现求s=4!+8!+5!+9!+2!+12!+11!
以下是Python实现求s=4!+8!+5!+9!+2!+12!+11!的代码:
```python
def jie(n):
if n == 1:
return 1
else:
return n*jie(n-1)
s = jie(4) + jie(8) + jie(5) + jie(9) + jie(2) + jie(12) + jie(11)
print(s)
```
输出结果为:```
4877120
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)