Sponsored

Friday, September 19, 2008

C PROGRAM FOR PASCALS TRIANGLE

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

/*PASCAL'S TRIANGLE PROGRAM */

#include
#include

int fact(int a) // function to calculate Factorial
{
int l,b=1;
if(a>1) // Condition used since factorial of 0 and 1 is 1
{
for(l=1;l<=a;l++)
b=b*l;
}
return b;
}

void main()
{
int i,j,k,n,m,c;
clrscr();
printf("Enter the number of lines for the pattern :: "); // Size of Pascal's triangle
scanf("%d",&n);
printf("\n\n");
m=n; // The number for formatting is same as the size of the triangle
for(i=0;i<=n;i++)
{
for(k=1;k<=m;k++)

printf(" ");
for(j=0;j<=i;j++)
{
c=(fact(i)/(fact(j)*fact(i-j))); // Formula for getting the value
printf("%d ",c);
}
m--; // Decrement counter on each iteration of the outer loop
printf("\n");
}
getch();
}

/* OUTPUT
Enter the number of lines for the pattern :: 4

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

*/

When we talk about an entry in Pascal's Triangle,we usually give a row number and a place in that row, beginning with row zero and place zero. For instance, the number 20 appears in row 6, place 3. Hence the output is 6 C3. The program above uses the same concept. Moving on in the same fashion from the 0th row, 0th column, Pascal’s triangle is constructed.


USE of PASCAL’S TRIANGLE


PROBABILITY

The logic above is that when you toss 1 coin, the output could be a head or a tail (1,1).

When you toss two coins together, both coins could have head, both could have tail or coin1 could have a head and coin2 a tail or coin1 a tail and coin2 a head (ie 1,2,1).


Algebra

Pascal's Triangle shows the coefficients in binomial expansion:

Here the coefficients of the binomial expansion correspond to the Pascal’s triangle


There are many other ways of coding Pascal's Triangle. Please be free to send in other techniques if you have them with you.

sponsored