读入任意非负整数N,输出N的阶乘c语言
时间: 2024-05-05 20:19:19 浏览: 20
实现:
```c
#include <stdio.h>
int main() {
int n, i;
unsigned long long factorial = 1;
printf("请输入一个非负整数:");
scanf("%d", &n);
for(i = 1; i <=n; ++i) {
factorial *= i;
}
printf("%d 的阶乘为 %llu", n, factorial);
return 0;
}
```
该程序会读入一个非负整数N,并计算出N的阶乘并输出。其中用到了一个for循环来计算N的阶乘,最后用printf函数将结果输出。
相关问题
编写函数计算阶乘,实现由键盘读入整数n,计算并输出n的阶乘。用C语言编程
好的,以下是一个计算阶乘的C语言函数:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int n;
printf("请输入一个整数n:");
scanf("%d", &n);
printf("%d的阶乘是:%d\n", n, factorial(n));
return 0;
}
```
这个函数接受一个整数n作为参数,利用for循环计算n的阶乘,并将结果返回。在main函数中,我们从键盘读入n,调用factorial函数计算阶乘并输出结果。
for循环求n的阶乘 c语言代码
好的,以下是使用for循环求n的阶乘的C语言代码:
```
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int n;
printf("请输入一个整数n:");
scanf("%d", &n);
int result = factorial(n);
printf("%d的阶乘为%d", n, result);
return 0;
}
```
这个程序定义了一个函数factorial来计算n的阶乘。在函数中,使用for循环从1开始迭代到n,每次将i乘以结果result,最后返回result的值。在主函数中,先读入n的值,然后调用factorial函数计算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)