编程实现:用 do…..while, while, for 三种循环语句求 n!(要求 n 的值从键盘输入), 的算法流程图和源程序
时间: 2024-06-13 17:07:36 浏览: 8
以下是用do-while、while、for三种循环语句求n!的算法流程图和源程序:
1. do-while循环语句求n!的算法流程图:
```
输入n
i=1, result=1
do
result=result*i
i=i+1
while(i<=n)
输出result
```
2. do-while循环语句求n!的源程序:
```c
#include <stdio.h>
int main()
{
int n, i = 1, result = 1;
printf("请输入n的值:");
scanf("%d", &n);
do
{
result = result * i;
i++;
} while (i <= n);
printf("%d! = %d\n", n, result);
return 0;
}
```
3. while循环语句求n!的算法流程图:
```
输入n
i=1, result=1
while(i<=n)
result=result*i
i=i+1
输出result
```
4. while循环语句求n!的源程序:
```c
#include <stdio.h>
int main()
{
int n, i = 1, result = 1;
printf("请输入n的值:");
scanf("%d", &n);
while (i <= n)
{
result = result * i;
i++;
}
printf("%d! = %d\n", n, result);
return 0;
}
```
5. for循环语句求n!的算法流程图:
```
输入n
result=1
for(i=1;i<=n;i++)
result=result*i
输出result
```
6. for循环语句求n!的源程序:
```c
#include <stdio.h>
int main()
{
int n, i, result = 1;
printf("请输入n的值:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
result = result * i;
}
printf("%d! = %d\n", n, result);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)