There are following rules must be made –

  1. Using 2-D array we can generate the Pascal’s triangle.
  2. The element at first column (Column Zero) must be filled up with one.
  3. The element which correspond to the same row and column will be filled up with one.
  4. Rest of the elements will be filled up with adding the elements of previous row as –
a[i][j]=a[i-1][j-1] + a[i-1][j];
#include<iostream.h>
#include<conio.h>
void main()
{
                int a[10][10],i,j,n;
                clrscr();
                cout<<"Enter The Number of Lines : ";
                cin>>n;
                for(i=0;i<n;i++)
                                {
                                for(j=0;j<=i;j++)
                                                {
                                                if((j==0) || (j==i))
                                                                {
                                                                a[i][j]=1;
                                                                }
                                                else
                                                                {
                                                                a[i][j]=a[i-1][j-1] + a[i-1][j];
                                                                }
                                                }
                                }
                cout<<"\nThe Pascal's Trianlge is : \n\n";
                for(i=0;i<n;i++)
                                {
                                for(j=0;j<=i;j++)
                                                {
                                                cout<<a[i][j]<<" ";
                                                }
                                cout<<endl;
                                }
                getch();
}

101

Advertisements