Pascal Triangle Pattern in Python language using loop
ADVERTISEMENTS
Pascal triangle pattern in python language using loop. In this article, you will learn how to print the pascal triangle pattern in python language.
Source Code
# Pascal Triangle Pattern in Python language using loop
r, co, ws, i, j = None, 1, None, None, None
# r - denotes the number of rows
# co - denotes the coefficient value
# ws - denotes the white space
print ("-----Enter the number of rows to print the pattern-----")
r = int (input ())
print ("\n-----This is the pascal triangle pattern-----\n")
for i in range (r):
print (end="\t")
for ws in range (1, (r + 1) - i):
print (end=" ")
for j in range (i + 1):
if j == 0 or i == 0:
co = 1
else:
co = co * (i - j + 1) / j
print (end="%4d" %(co))
print (end="\n")
Output
-----Enter the number of rows to print the pattern-----
7
-----This is the pascal triangle pattern-----
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1