用C语言编写函数,计算s=5!+7!+10!
时间: 2023-07-16 19:15:01 浏览: 111
以下是用C语言编写的函数,计算s=5!+7!+10!:
```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(5) + factorial(7) + factorial(10);
printf("s = %d\n", s);
return 0;
}
```
首先定义了一个计算阶乘的函数 `factorial`,然后在 `main` 函数中调用该函数计算出 5!、7! 和 10!,并将它们相加得到结果 s。最后用 `printf` 函数输出结果。
相关问题
c语言编写函数,计算n!,int fun(int n) 在main函数中调用函数计算: S=1! +2! +3!+......+10!
以下是C语言编写函数,计算n!的代码和在main函数中调用函数计算S=1!+2!+3!+...+10!的代码:
```c
#include <stdio.h>
// 计算n的阶乘
int fun(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int n = 10;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += fun(i);
}
printf("S = %d\n", sum); // 输出:S = 4037913
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;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)