用c语言打印杨辉三角的前十行
时间: 2023-08-27 16:06:59 浏览: 57
好的,以下是用C语言打印杨辉三角前十行的代码:
```
#include<stdio.h>
int main()
{
int rows, i, j, coef;
printf("Enter the number of rows: ");
scanf("%d",&rows);
printf("The Pascal's Triangle for the first %d rows is:\n",rows);
for(i=0;i<rows;i++)
{
coef = 1;
for(j=1;j<=rows-i;j++)
printf(" ");
for(j=0;j<=i;j++)
{
printf("%4d",coef);
coef = coef * (i - j) / (j + 1);
}
printf("\n");
}
return 0;
}
```
希望能解答您的问题!
相关问题
用c语言打印杨辉三角前十行
以下是用 C 语言打印杨辉三角前十行的代码:
```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("%d ", coef);
}
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
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
```
用c语言打印杨辉三角前十行左对齐
以下是使用C语言打印杨辉三角前十行左对齐的代码:
```c
#include <stdio.h>
int main() {
int i, j, n = 10;
int arr[n];
for (i = 0; i < n; i++) {
arr[0] = 1;
arr[i] = 1;
for (j = i - 1; j > 0; j--) {
arr[j] = arr[j - 1] + arr[j];
}
for (j = 0; j <= i; j++) {
printf("%-4d", arr[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
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
```
相关推荐
![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)