Sunday, 9 March 2014

'C' Code to print Pascal's Triangle

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

main()
{
 int n,i,j;
 printf("Enter the number of rows of pattern to be drawn: ");
 scanf("%d",&n);
 
 for(i=0;i<=n-1;i++)
 {
  for(j=1;j<=n-i-1;j++)
  {
   printf(" ");
  }
  for(j=0;j<=i;j++)
  {
   if(i==j || j==0)
   printf("1 ");
   else
      printf("%d ",f(i,j));
      
  }
  printf("\n");
 }
 
}

f(x,y)
  int x , y;
{
 int z = x - y , fx , fy , fz;
 fx=1;
 while(x!=1)
 {
  fx = fx * x;
  x--;
 }
 fy = 1;
 while(y! = 1)
 {
  fy = fy * y;
  y--;
 }
  fz = 1;
 while(z!=1)
 {
  fz = fz * z;
  z--;
 }
 
 return fx / (fy * fz);
 
}