使用do-while语句,编写程序,实现 任意输入n,求n的阶乘(n<20)。C语言
时间: 2024-06-03 18:09:25 浏览: 11
好的,这是一个计算阶乘的问题。以下是C语言代码实现:
#include <stdio.h>
int main() {
int n, i = 1;
long long result = 1;
printf("请输入一个小于20的正整数n:");
scanf("%d", &n);
do {
result *= i;
i++;
} while (i <= n);
printf("%d的阶乘为%lld", n, result);
return 0;
}
希望这个程序对你有帮助!
相关问题
c语言编程实现,用do-while语句求n!的值
以下是用 do-while 语句求 n! 的 C 语言代码:
```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;
}
```
这段代码可以计算 n 的阶乘,其中 do-while 循环用于计算阶乘的值。
c语言用while语句求n的阶乘
以下是使用while语句求n的阶乘的C语言代码:
```c
#include <stdio.h>
int main() {
int n, result = 1;
printf("请输入一个正整数n:");
scanf("%d", &n);
int i = 1;
while (i <= n) {
result *= i;
i++;
}
printf("%d的阶乘为%d\n", n, result);
return 0;
}
```
解释:首先输入一个正整数n,然后定义一个变量result并初始化为1。接着定义一个变量i并初始化为1,使用while循环,当i小于等于n时,执行循环体内的语句,即将result乘以i,然后i加1。当i大于n时,跳出循环。最后输出n的阶乘结果。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)