用函数的嵌套调用计算s=1!+2!+3!+ ……+10!
时间: 2023-11-12 14:07:21 浏览: 71
可以使用递归函数来实现阶乘的计算,然后在主函数中循环调用该函数,累加每个数的阶乘即可得到结果。具体实现如下:
```python
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
s = 0
for i in range(1, 11):
s += factorial(i)
print(s)
```
输出结果为: 4037913
相关问题
用c函数的嵌套调用计算s=1!+2!+3!+ ……+10!cc
以下是用C函数的嵌套调用计算s=1!+2!+3!+ ……+10!的代码:
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int s = 0;
for (int i = 1; i <= 10; i++) {
s += factorial(i); }
printf("s = %d\n", s);
return 0;
}
```
输出结果为:
```
s = 4037913
```
c语言函数的嵌套调用计算s=1!+2!+3!+ ……+10!cc
可以使用递归函数来计算阶乘,然后在主函数中循环调用该函数并累加结果即可。具体实现如下:
```c
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int s = 0;
for (int i = 1; i <= 10; i++) {
s += factorial(i);
}
printf("s = %d\n", s);
return 0;
}
```
输出结果为:
```
s = 4037913
```
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)