用c语言计算1!+2!+3!+……+
时间: 2023-05-21 19:03:27 浏览: 84
可以使用循环结构来计算1!2!3!……的和,代码如下:
```c
#include <stdio.h>
int main() {
int n = 10; // 计算前10个数的阶乘
int sum = 0;
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
sum += factorial;
}
printf("1! + 2! + 3! + ... + %d! = %d\n", n, sum);
return 0;
}
```
输出结果为:
```
1! + 2! + 3! + ... + 10! = 4037913
```
相关问题
c语言计算 1!+2!+3!+……+10!
以下是两种C语言计算1!+2!+3!+……+10!的例子:
1.使用上一个计算的阶乘进行相乘,避免每次都重新计算阶乘:
```c
#include <stdio.h>
int main() {
int i = 0;
int result = 1;
int sum = 0;
for (i = 1; i < 11; i++) {
result *= i; //result会保留上一个的阶乘进行相乘
sum += result;
}
printf("1!+2!+3!+……+10!:%d", sum);
return 0;
}
```
2.每次都重新计算阶乘,避免结果越来越大:
```c
#include <stdio.h>
int main() {
int i = 0;
int j = 0;
int n = 0;
int sum = 0;
int result = 1;
for ( i = 1; i < 11; i++) {
result = 1; //计算下一个阶乘时,要将result初始化
for (j = 1; j <= i; j++) {
result *= j;
}
sum += result;
}
printf("1!+2!+3!+……+10!:%d", sum);
return 0;
}
```
c语言编程计算1!+2!+3!+……+6!
以下是两种C语言编程计算1!+2!+3!+……+6!的例子:
引用:
```c
#include <stdio.h>
int main() {
int i = 0;
int result = 1;
int sum = 0;
for (i = 1; i < 7; i++) {
result *= i;
sum += result;
}
printf("1!+2!+3!+……+6!:%d", sum);
return 0;
}
```
引用:
```c
#include <stdio.h>
int main() {
int i = 0;
int j = 0;
int n = 0;
int sum = 0;
int result = 1; for ( i = 1; i < 7; i++) {
result = 1;
for (j = 1; j <= i; j++) {
result *= j;
}
sum += result;
}
printf("1!+2!+3!+……+6!:%d", sum);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.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_lunwen.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)