"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Wednesday, January 13, 2010

Pascal Triangle

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1


#include<iostream.h>
#include<conio.h>
#include<process.hgtgt;

void main()
{
int a[10][10];
int i,j,c,n;
clrscr();

cout<<"Enter the height(below 11): ";
cin>>n;
if(n>10 || n<1)
{cout<<"\n Wrong input";
getch();
exit(1);
}

a[0][0]=1;
a[1][0]=1;
a[1][1]=2;
a[1][2]=1;


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

//display

int sp = n;
for(int s = 0; s <=sp; s++)
cout<<" ";
cout<<a[0][0]<<"\n";
sp=sp-2;
for(s = 0; s <=sp; s++)
cout<<" ";
cout<< a[1][0]<<" " << a[1][1] <<" "<<a[1][2];
cout<<"\n";
for(i=2;i < n ; i++ )
{
sp--;
for( s = 0; s <=sp; s++)
cout<<" ";
cout<< a[i][0] <<" ";
j=1,c=2;
while(j<=i)
{cout<<a[i][j] <<" ";
j++;
c++;}
cout<< a[i][j] << "\n";
}

getch();
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.