Tuesday, 30 April 2013

PASCAL'S TRIANGLE


One of the most interesting Number Patterns is Pascal's Triangle (named after Blaise Pascal, a famous French Mathematician and Philosopher).

To build the triangle, start with "1" at the top, then continue placing numbers below it in a triangular pattern.

Each number is just the two numbers above it added together (except for the edges, which are all "1").

(Here I have highlighted that 1+3 = 4)


So Pascal's Triangle could also be an "n choose k" triangle like this:


Note that the top row is row zero and also the leftmost column is zero.



SOURCE CODE:-
/***********************************************************************/
#include<stdio.h>
#include<conio.h>

double nCr(int,int);
double factorial(int);

int main()
{
int r,i,j;
clrscr();
printf("ENTER THE NUMBER OF ROWS : ");
scanf("%d",&r);

for(i=0;i<r;i++)
{
for(j=0;j<=i;j++)
{
if(i==j)
{
printf("1\t");
}
else
{
printf("%0.0lf\t",nCr(i,j));
}
}
printf("\n");
}

getch();
return 1;
}


double nCr(int i,int j)
{
return factorial(i)/(factorial(j)*factorial(i-j));
}

double factorial(int n)
{
double fact=1;
int i=1;
for(i=1;i<=n;i++)
fact*=i;

return fact;
}

/*********************************************************************/


1 comment: