用 c语言do…..while, while, for 三种循环语句求 n!(要求 n 的值从键盘输入), 输出结果。
时间: 2024-06-13 20:07:37 浏览: 15
以下是三种循环语句求n!的C语言程序,其中n的值从键盘输入:
1.使用do...while循环语句
```c
#include <stdio.h>
int main()
{
int n, i = 1, result = 1;
printf("请输入一个整数n:");
scanf("%d", &n);
do {
result *= i;
i++;
} while (i <= n);
printf("%d的阶乘为:%d\n", n, result);
return 0;
}
```
2.使用while循环语句
```c
#include <stdio.h>
int main()
{
int n, i = 1, result = 1;
printf("请输入一个整数n:");
scanf("%d", &n);
while (i <= n) {
result *= i;
i++;
}
printf("%d的阶乘为:%d\n", n, result);
return 0;
}
```
3.使用for循环语句
```c
#include <stdio.h>
int main()
{
int n, i, result = 1;
printf("请输入一个整数n:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
result *= i;
}
printf("%d的阶乘为:%d\n", n, result);
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)