Sunday, September 25, 2016

PASCAL TRIANGLE:

Tags

C++ Program to Display Pascal Triangle.

#include <iostream>
using namespace std;
int main()
{
int a[20][20],i=0,j=0,n;
cout<<"Enter Size of Pascal Triangle: ";
cin>>n;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
a[i][j]=0;
}

a[0][0]=1;  //Making First and Second Rows as 1's
a[1][0]=1;
a[1][1]=1;

for(i=2;i<n;i++)
{
a[i][0]=1;
for(j=1;j<n;j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
}

cout<<"\nPascal Triangle is:\n\n";
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
cout<<a[i][j];
cout<<"\t";
}
cout<<endl;
}
return 0;
}