c语言用二维数组遍写杨辉三角
时间: 2023-12-25 14:10:55 浏览: 81
c语言杨辉三角 (二维数组).zip
#include <stdio.h>
int main()
{
int rows, coef = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
int triangle[rows][rows];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j <= i; j++)
{
if(j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
triangle[i][j] = coef;
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j <= i; j++)
{
printf("%d ", triangle[i][j]);
}
printf("\n");
}
return 0;
}
阅读全文