Sunday, August 19, 2007

Interesting C Program -15

Atlast, Here is my implementation of Pascal Triangle!!!

/*PASCAL TRIANGLE*/
#include"stdio.h"
int factorial(int);
int combination(int,int);
int main()
{
int rows,i,j;
printf("ENTER THE NUMBER OF ROWS:\t");
scanf("%d",&rows);
printf("\n---------------------------------------------------------\
------------------\n");
printf(" PASCAL TRIANGLE WITH %d ROWS \ ",rows);
printf("\n-----------------------------------------------------------\
----------------\n");
for(i=1;i<=rows;i++)
{
for(j=0;j<=i;j++)
{
printf("%d\t",combination(i,j));
}
//printf("\n");
printf("\n---------------------------------------------------------------\
------------\n");
}
}
int factorial(int x)
{
if(x<2)
return 1;
else
{
return x*factorial(x-1);
}
}
int combination(int n,int r)
{
int y;
y=factorial(n)/(factorial(r)*factorial(n-r));
return y;
}


The Output looks like this

ENTER THE NUMBER OF ROWS: 5

---------------------------------------------------------------------------
PASCAL TRIANGLE WITH 5 ROWS
---------------------------------------------------------------------------
1 1
---------------------------------------------------------------------------
1 2 1
---------------------------------------------------------------------------
1 3 3 1
---------------------------------------------------------------------------
1 4 6 4 1
---------------------------------------------------------------------------
1 5 10 10 5 1
---------------------------------------------------------------------------

No comments:

Search Google

Books that I refer to...

  • The Complete Reference C, Fourth Edition
  • The C Programming Language