My Photo
Name:
Location: Bangalore, India
Google

Tips to Reduce Abdominal Fat!

Thursday, October 27, 2005

Pascal's Triangle

The r th element of Pascal's Triangle at n th row is computed as

m = n!/(r!*(n-r)!)

Typical o/p would be
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

A typical program to print Pascal's Triangle developed in c goes as below.

#include
extern long fact(int n);
void PascalsTriangle()
{
int n,r,i;
long m;

n=r=i=0;
m=1;

printf("Enter the no. of rows required");
scanf("%d",&n);

for(i=0; i< n;i++)
{
for(r=0;r<=i;r++)
{
m = fact(i)/(fact(r) * fact(i-r));
printf("%4d",m);
}
printf("\n");
}
}

/*Function: Factorial
Use: This would return the factorial of the number passed.*/

long fact(int n)
{
long f;
f=n;

if(n<=1)
return (1);

while (n>1)
{
f=f*(n-1);
n--;
}
return (f);
}

0 Comments:

Post a Comment

<< Home