Thursday 13 September 2012

To generate Pascal’s triangle


Description:
Pascal’s triangle which is used for a coefficient in the equation in polynominals.
Alogrithm:
            Step 1:  Start
 Step 2:  Initialize m=0
 Step 3:  Read  n
 Step 4:  If m<n goto step 5.if not goto step 12
 Step 5:  initialize i=40-m
 Step 6:  If i>0 is true do as follows. If not goto step 7
                                                    i.      print white space
                                                  ii.      decrement i
                                                iii.      goto Step 6
 Step 7: Initialize j=0
 Step 8: If j=m do as follows. If not goto Step 10
i)                    if(j==0||m==0)
ii)                  Initialize b=1 if not b=b*(m-j+1)/j
iii)                Print white space, b .
iv)                Goto Step 9
 Step 9:  increment j, goto Step 8
 Step 10: print new line control
 Step 11: increment m, goto step 4
 Step 12: Stop
 #include<stdio.h>
#include<conio.h>
void main()
{
            int i,p,r,x,binom=1;
  clrscr();
             printf("enter the how many lines to print");
             scanf("%d",&p);
  i=0;
  while(i<p) // check the condition
             {
                         for(r=40-i;r>0;r--)  // perform the looping operation until 0
                        printf(" ");
                         for(x=0;x<=i;x++)
                          {
                                    if((x==0)||(i==0)) // check the condition
                                    binom=1;
                                    else
                                    binom=binom*(i-x+1)/x;
                                    printf("%d",binom);
                                    printf(" ");
                         }
                         printf("\n");
                         i++;
  }
  getch();
}

Output:
1.enter the how many lines to print5
                                        1
                                       1 1
                                      1 2 1
                                     1 3 3 1
                                    1 4 6 4 1

2.enter the how many lines to print3
                                        1
                                       1 1
                                      1 2 1
Conclusion: the program is error free



VIVA QUESATIONS:
1) What is meant by Pascal’s triangle ?

Ans: Pascal’s triangle which is used for a coefficient in the equation in polynominals
2)define structure ?
Ans: A structure in c is a heterogenous user efined data type. A structure may contain different data types.It groups variables into a single entity.


No comments:

Post a Comment