Tuesday 8 November 2011

Write a program to print Pascal triangle

Algorithm:

 Step1:         start

Step2:         initialize binom =1, f=0

Step3:         check the condition f<p

Step4:         for r=40-3*f;r>0; --r

Step5:         print" "

Step6:         for x=0; x<f; ++x

Step7:         if x == 0 or f==0

          Binom = 1


Step8:         else

          Binom=(bimom*(f-x+1)/x


          Printf binom


Step9:         stop

 

Program:

#include<stdio.h>

main()

{

int binom, p,f,r,x;


clrscr();


binom=1;


f=0;


pirntf("\nEnter the no.of rows : ");


scanf("%d",&p);


printf("\nPascal Triangle");


while(f<p)


{


for(r=40-3*f;r>0;--r)


printf(" ");


for(x=0;x<=f;++x)


{


if((x==0) || (f==0))


binom = 1;


else


binom = (binom *(f-x+1))/x;


printf("%d",binom);


}


          printf("\n");


          ++f;


}


getch();


}

Output:

Enter the no.of rows : 5

1


1           1


1           2           1


1           3           3          1


1          4           6            4         1

5 comments: