C语言输出6行杨辉三角形。
时间: 2024-02-13 21:07:45 浏览: 34
以下是C语言代码实现输出6行杨辉三角形:
```c
#include <stdio.h>
int main() {
int n = 6; // 输出6行杨辉三角形
int arr[n][n];
// 初始化杨辉三角形数组
for(int i = 0; i < n; i++) {
for(int j = 0; j <= i; j++) {
if(j == 0 || j == i) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
}
}
// 输出杨辉三角形
for(int i = 0; i < n; i++) {
for(int j = 0; j <= i; j++) {
printf("%d ", arr[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语言输出6行杨辉三角形
好的,以下是用C语言输出6行杨辉三角形的代码:
```c
#include <stdio.h>
int main() {
int numRows = 6; // 杨辉三角形的行数
int triangle[numRows][numRows]; // 定义二维数组存储杨辉三角形的值
// 初始化第一列和对角线的值为1
for (int i = 0; i < numRows; i++) {
triangle[i][0] = 1;
triangle[i][i] = 1;
}
// 计算杨辉三角形的其他值
for (int i = 2; i < numRows; i++) {
for (int j = 1; j < i; j++) {
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
}
}
// 输出杨辉三角形
for (int i = 0; i < numRows; 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语言输出10行杨辉三角形
以下是输出10行杨辉三角形的C语言代码:
#include <stdio.h>
int main() {
int rows = 10;
int 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("%4d", coef);
}
printf("\n");
}
return 0;
}
相关推荐
![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)