#pascal triangle for n height
from __future__ import print_function
h=int(raw_input("Enter Height of triangle : "));
tri=[1,1,1];
index=1;
for x in range(2,h):
tri.append(1);
for y in range(index,index+x-1):
tri.append(tri[y]+tri[y+1]);
index=index+x;
tri.append(1);
print (tri);
index=0;
for x in range(0,h) :
for m in range(x,h) :
print (" ",end="");
for y in range(index,(index+x)+1) :
print (tri[y]," ",end="");
index=index+x+1;
print ("");
To Run : python pascal.py
The output would be like this :
In ubuntu Terminal :

No comments:
Post a Comment