Archive for November, 2010

/* Program to print Pascal triangle */
/* Here nCr=n!/((n-r)!r!)=n*(n-1)*(n-2)………(n-r)/(1*2*3*………..(n-r)) */
#include<stdio.h>

long factorial(int);
void space(int);

main()
{
int i,j,rows;
printf(“\nNo of rows: “);
scanf(“%d”,&rows);
for(i=0;i<rows;i++)
{
space(3*(rows-i));
for(j=0;j<=i;j++)
{
printf(“%3d”,nCr(i,j)); // printing nCr value
space(3);
}
printf(“\n”);
}
}

int nCr(int n,int r) // calculating the value of nCr
{
int i,ncr=1;
for(i=1;i<=n-r;i++)
{
ncr=ncr*(n-i+1)/i;
}
return ncr;
}

void space(int n)
{
int i;
for(i=0;i<n;i++)
printf(” “);
}
Logic

nCr=nCr=n!/((n-r)!r!)=n*(n-1)*(n-2)………(n-r)/(1*2*3*………..(n-r))

Pascal Triangle can be represented with nCr as follows:

0C0

1C0    1C1

2C0     2C1     2C2

…………………………….

…………………………….


(adsbygoogle = window.adsbygoogle || []).push({});

Advertisements