C语言求1!+2!+3!+……n!用while语句
时间: 2023-11-24 15:05:54 浏览: 57
以下是使用while语句求1!+2!+3!+……n!的C语言代码:
```c
#include <stdio.h>
int main() {
int i = 1;
int result = 1;
int sum = 0;
int n = 10; // 求1!+2!+3!+……+10!
while (i <= n) {
result *= i;
sum += result;
i++;
}
printf("1!+2!+3!+……+10!:%d", sum);
return 0;
}
```
相关问题
C语言解决do...while语句计算1!+3!+5!+……+13!问题
好的,这个问题可以回答。以下是C语言代码示例,用do...while语句计算1! 3! 5! …… 13!:
```c
#include <stdio.h>
int main() {
int n = 1, fac = 1;
do {
fac *= n;
n += 2;
} while (n <= 13);
printf("%d\n", fac);
return 0;
}
```
输出结果是:135135,即1! * 3! * 5! * ... * 13! 的值。
编写c语言程序1+2+3……+100使用while语句
非常感谢您的问题!以下是一个使用 while 循环打印 1 到 100 的 C 语言程序:
```
#include <stdio.h>
int main() {
int i = 1;
while (i <= 100) {
printf("%d ", i);
i++;
}
return 0;
}
```
这个程序会首先初始化变量 i 为 1,然后进入一个 while 循环。只要 i 小于或等于 100,循环就会一直执行。
在循环的主体中,我们使用 printf 函数打印当前 i 的值。然后通过 i++ 语句将 i 的值增加 1,这样下次循环时就会打印出下一个数字。
当 i 的值变为 101 时,循环终止,程序结束。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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_column_c1.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)
![](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)