使用for循环计算1!+2!+3!+...+10!的结果。 题目分析: (1)题目求阶乘的和。同样考虑到i循环,如输入3,sum=1!+2!+3!,因2!=2*1 (2)因考虑到阶乘不能与0相乘,所以
时间: 2023-11-17 12:59:52 浏览: 26
需要将sum初始化为1,而不是0。
代码实现:
int sum = 1;
int factorial = 1;
for(int i=2; i<=10; i++){
factorial *= i;
sum += factorial;
}
因此,1!+2!+3!+...+10!的结果为sum=4037913。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)