C语言编程打印杨辉三角形的前6行6列的值。即输出:1112113311464115101051
时间: 2023-06-02 19:05:50 浏览: 71
下面是C语言代码实现:
#include <stdio.h>
int main() {
int triangle[6][6] = {0}; // 初始化为0
int i, j;
// 初始化第一列和对角线为1
for (i = 0; i < 6; i++) {
triangle[i][0] = 1;
triangle[i][i] = 1;
}
// 根据杨辉三角的性质,填充其它元素
for (i = 2; i < 6; i++) {
for (j = 1; j < i; j++) {
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
}
}
// 输出前6行6列的值
for (i = 0; i < 6; i++) {
for (j = 0; j <= i; j++) {
printf("%d", triangle[i][j]);
}
printf("\n");
}
return 0;
}
相关问题
c语言,编程打印杨辉三角形前六行六列的值
好的,以下是C语言代码实现杨辉三角形前六行六列的值的打印:
```c
#include <stdio.h>
int main() {
int rows = 6;
int triangle[rows][rows];
// 初始化杨辉三角
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) {
triangle[i][j] = 1;
} else {
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
}
}
}
// 打印杨辉三角
for (int i = 0; i < rows; i++) {
for (int j = 0; j <= i; j++) {
printf("%d ", triangle[i][j]);
}
printf("\n");
}
return 0;
}
```
输出结果为:
```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
```
c语言,编程输出杨辉三角形前六行六列的值
当然,我可以帮你回答这个问题!以下是输出杨辉三角形前六行六列的C语言代码:
```c
#include <stdio.h>
int main()
{
int rows = 6, coef = 1;
for(int i = 0; i < rows; i++)
{
for(int space = 1; space < rows - i; space++)
printf(" ");
for(int j = 0; j <= i; j++)
{
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%6d", coef);
}
printf("\n");
}
return 0;
}
```
希望对你有帮助!
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)