Sunday, 15 March 2015

Program to generate Pascal's triangle Using C

# include <stdio.h>
# include <conio.h>

void main()
{
int b, p, q, r, x ;
clrscr() ;
b = 1 ;
q = 0 ;
printf("Enter the number of rows : ") ;
scanf("%d", &p) ;
printf("\nPascal's triangle is : \n\n") ;
while (q < p)
{
for(r = 40 - 3 * q ; r > 0 ; --r)
printf(" ") ;
for(x = 0 ; x <= q ; ++x)
{
if((x == 0) || (q == 0))
b = 1 ;
else
b = (b * (q - x + 1)) / x ;
printf("%6d", b) ;
}
printf("\n\n") ;
++q ;
}
getch() ;
}

RUN 1 :
~~~~~~~
Enter the number of rows : 5
Pascal's triangle is :
      1
    1 1
   1 2 1
  1 3 3 1
1 4 6 4 1

No comments:

Post a Comment