Pascal Triangle using 2D array
//Pascal Triangle .... #include<stdio.h> int main() { int ar[10][10],m,n,i,j; printf("\nenter the height of the pascal triangle : "); scanf("%d",&m); for(i=0;i<m;i++) for(j=0;j<m;j++) { if(j==0) ar[i][j]=1; else ar[i][j]=0; } for(i=1;i<m;i++) { for(j=1;j<=i;j++) { ar[i][j]=ar[i-1][j-1]+ar[i-1][j]; } } printf("\nPscal Triangle will look like : \n"); for(i=0;i<m;i++) { for(j=0;j<20-i;j++) printf(" "); for(j=0;j<=i;j++) printf("%2d",ar[i][j]); printf("\n"); } } O/P:
Comments
Post a Comment