#include <stdio.h>
// Calculate Pascal Triangel with NxN array size
#define LineNum 11   // the number of line in the array ( N )
void main(){
  int  pascalArr[LineNum+1][LineNum+1];
  int  i=1 , j=1 ;
  pascalArr[1][1]=1;
  for (i=1 ; i<=LineNum ; i++) { 
	pascalArr[i][1]=pascalArr[i][i]=1;
	for (j=2 ; j<i ; j++) pascalArr[i][j] = pascalArr[i-1][j-1]+pascalArr[i-1][j];
  }
  for (i=1;i<=LineNum;i++){ // print the output
      for (j=1;j<=i;j++) printf(" %3d ",pascalArr[i][j]);
      printf("\n");
  }
}
Published in: C++
Download

Related snippets