c语言调用n的阶乘的函数打印杨辉三角
时间: 2023-12-02 15:03:13 浏览: 85
好的,下面是使用C语言调用n的阶乘函数打印杨辉三角的代码:
```c
#include <stdio.h>
// 定义阶乘函数
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int n;
printf("请输入要打印的杨辉三角的行数: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
// 计算杨辉三角的每个数值
int val = factorial(i) / (factorial(j) * factorial(i - j));
printf("%d ", val);
}
printf("\n");
}
return 0;
}
```
运行结果示例:
```
请输入要打印的杨辉三角的行数: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
希望能够帮到你!
相关问题
用C语言调用函数求n的阶乘
C语言中可以使用循环来计算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 = 5;
int result = factorial(n);
printf("%d! = %d\n", n, result);
return 0;
}
```
输出结果为:
```
5! = 120
```
在上面的代码中,我们定义了一个名为`factorial`的函数,该函数的参数为一个整数n,返回值为n的阶乘。在函数中,我们使用了一个循环来计算阶乘,最后返回结果。在主函数中,我们定义了n的值为5,并调用了`factorial`函数来计算5的阶乘。
c语言计算n的阶乘函数
以下是两种C语言计算n的阶乘函数的例子:
1.创建函数来实现,函数里的计算方法实际还是循环
```c
#include<stdio.h>
int jie(int n) //创建一个jie函数用来求n!
{
int i, b=1;
for (i = 1; i <= n; i++)
{
b *= i;
}
return b;
}
int main()
{
int n = 0;
scanf("%d", &n);
int a = jie(n);
printf("%d\n", a);
}
```
2.用循环直接算
```c
#include <stdio.h>
int main()
{
int n,i,a=1;
scanf("%d",&n);
for (i = 1; i <=n; i++)
{
a= a*i;
}
printf("%d\n", a);
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)