#include
/*program to generate pascal triangle, no. of lines r to be entered by the user*/
void main()
{
int a,b,c,i,j,n,r,ncr,factr,factn,factnr,line;
printf("please enter the number of lines of the pascal triangle\n");
scanf("%d",&line); //inputs the no. of lines we want
clrscr();
for(i=0;i<=line-1;i++)//outer loop for the value of n
{
factn=1;// reinitializing the factorial values to avoid garbage values...
factr=1;
factnr=1;
for(j=0;j<=i;j++) //outer loop for the values of r
{
n=i; //here we have assigned i to n and j to r for our nCr calculation and convinience
r=j;
factn=1; // again we have re initialised factorial values to avoid garbage values
factr=1;
factnr=1;
if(n==0) //we know factorial of 0 =1
factn=1;
else
{
for(a=n;a>=1;a--) //factorial calculation of n is n!=0
factn=factn*a;
}
if(r==0)
factr=1;
else
{
for(b=r;b>=1;b--) //factorial calculation of r if r!=0
factr=factr*b;
}
if((n-r)==0)
factnr=1;
else
{
for(c=(n-r);c>=1;c--) //factorial calculation of n-r if (n-r)!=0
factnr=factnr*c;
}
ncr=factn/(factr*factnr); //ncr calculation
printf("%d\t",ncr); //prints the value of ncr
}
printf("\n"); //cursor get to the next line when the control exits the inner loop
}
}
No comments:
Post a Comment