c语言输出杨辉三角形的前十行:
时间: 2023-11-14 20:20:35 浏览: 49
好的,以下是输出杨辉三角形前十行的C语言代码:
```
#include <stdio.h>
int main()
{
int rows, coef = 1;
printf("前十行杨辉三角形:\n");
for(int i=0; i<10; i++)
{
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;
}
```
希望能帮到您。
相关问题
c语言输出杨辉三角形前十
以下是C语言输出杨辉三角形前十的两种方法:
方法一:使用二维数组实现
```c
#include <stdio.h>
int main() {
int i, j;
int a[10][10];
printf("\n");
for (i = 0;i < 10;i++) {
a[i][0] = 1;
a[i][i] = 1;
}
for (i = 2;i < 10;i++)
for (j = 1;j < i;j++)
a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
for (i = 0;i < 10;i++) {
for (j = 0;j <= i;j++)
printf("%-5d", a[i][j]);
printf("\n");
}
}
```
方法二:使用递归实现
```c
#include <stdio.h>
int fun(int i,int j){
if(j==0||i==j){
return 1;
}else{
return fun(i-1,j)+fun(i-1,j-1);
}
}
void main(){
int n,i,j;
for(i=0;i<10;i++){
for(j=0;j<=i;j++){
printf("%d\t",fun(i,j));
}
printf("\n");
}
}
```
用c语言输出杨辉三角形的前10行。
以下是输出杨辉三角形前10行的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
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.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)