c语言杨辉三角
时间: 2023-06-21 15:18:53 浏览: 58
以下是用 C 语言实现杨辉三角的代码:
```
#include <stdio.h>
int main() {
int rows, coef = 1, space, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
for (space = 1; space <= rows - i; space++)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
```
运行程序后,输入杨辉三角的行数,就可以输出相应的杨辉三角了。
相关问题
c语言 杨辉三角
以下是C语言实现杨辉三角的代码:
```c
#include <stdio.h>
int main() {
int n, i, j, coef = 1;
printf("Enter the number of rows: ");
scanf("%d", &n);
for(i=0; i<n; i++) {
for(j=0; j<=i; j++) {
if(j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;
printf("%d ", coef);
}
printf("\n");
}
return 0;
}
```
运行程序后,输入需要打印的行数,即可输出杨辉三角形式的数字。
c语言杨辉三角形pta
以下是一个简单的C语言杨辉三角形的代码实现,可以通过PTA的测试:
```c
#include <stdio.h>
int main() {
int n, i, j;
int a[10][10];
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
if (j == 0 || i == j) {
a[i][j] = 1;
} else {
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
printf("%4d", a[i][j]);
if (i == j) {
printf("\n");
}
}
}
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)