c语言双循环求 3!+4!+5!+6!+7!=?
时间: 2023-11-24 13:49:17 浏览: 48
这道题目可以使用双重循环来解决。其中外层循环控制数字的范围,内层循环计算每个数字的阶乘并将其累加到总和中。根据引用中的方法,我们可以直接在内层循环中计算阶乘并将其累加到总和中。具体代码如下:
#include <stdio.h>
int main() {
int i = 0;
int set = 1;
int n = 0;
int sum = 0;
for (n = 3; n <= 7; n++) {
set = 1;
for (i = 1; i <= n; i++) {
set = set * i;
}
sum = sum + set;
}
printf("%d\n", sum);
return 0;
}
因此,3!+4!+5!+6!+7!=4356。
相关问题
c语言双循环求1: 3!+4!+5!+6!+7!=?
这道题目可以使用双重循环来解决。其中外层循环控制数字的范围,内层循环计算每个数字的阶乘并将其累加到总和中。根据引用中的方法,我们可以直接在一个循环中计算所有数字的阶乘并将其累加到总和中。因此,我们可以使用以下代码来解决这个问题:
```
#include <stdio.h>
int main() {
int n = 7; // 求1: 3!+4!+5!+6!+7!
int i, j, sum = 0, set = 1;
for (i = 3; i <= n; i++) {
set = 1;
for (j = 1; j <= i; j++) {
set *= j;
}
sum += set;
}
printf("%d\n", sum);
return 0;
}
```
C语言求1!+2!+3!+4!+...+20!
以下是C语言求1!+2!+3!+4!+...+20!的代码演示:
```c
#include <stdio.h>
int main()
{
double sum = 0, temp = 1;
int i;
for (i = 1; i <= 20; i++)
{
temp = temp * i;
sum = sum + temp;
}
printf("结果:%22.15e\n", sum);
return 0;
}
```
该程序使用了for循环来计算1!+2!+3!+4!+...+20!,并使用了双精度浮点型变量来存储计算结果。在每次循环中,变量temp存储当前循环变量i的阶乘,变量sum则将每次计算的阶乘结果相加。最后,使用printf函数输出计算结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)