c语言求阶乘 求1!+2!+…+20!
时间: 2023-11-27 09:06:17 浏览: 45
以下是C语言求阶乘的例子:
```c
#include <stdio.h>
int main() {
int n = 5;
int result = 1;
for(int i = 1; i <= n; i++) {
result *= i;
}
printf("%d的阶乘是%d", n, result); // 输出:5的阶乘是120
return 0;
}
```
以下是C语言求1!+2!+…+20!的例子:
```c
#include <stdio.h>
int main() {
double sum = 0;
double j = 1;
for(int n = 1; n <= 20; n++) {
j = 1;
for(int i = 1; i <= n; i++) {
j *= i;
}
sum += j;
}
printf("1!+2!+...+20!=%lf", sum); // 输出:1!+2!+...+20!=2561327494111820313.000000
return 0;
}
```
相关问题
用c语言求1!+2!+...+20!的阶乘
以下是用C语言求1!+2!+...+20!的阶乘的代码实现:
```c
#include <stdio.h>
int main() {
int i, j, sum = 0, m = 1;
for (i = 1; i <= 20; i++) {
m *= i;
sum += m;
}
printf("1!+2!+...+20! = %d\n", sum);
return 0;
}
```
c语言用递归方法求1!+2!+10!
以下是C语言用递归方法求1!+2!+10!的代码:
```c
#include <stdio.h>
int factorial(int n) { // 定义递归函数,求n的阶乘
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n = 10; // 求1!+2!+...+10!
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += factorial(i); // 累加每个数的阶乘
}
printf("1!+2!+...+10!=%d\n", sum); // 输出结果
return 0;
}
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)