How to print Pascal…
 
Notifications
Clear all

How to print Pascal’s triangle?

1 Posts
2 Users
0 Likes
368 Views
0
Topic starter

How to print Pascal’s triangle?

1 Answer
0

Here’s a Python code snippet to print Pascal’s triangle:

# define the number of rows to print
num_rows = 5

# initialize the triangle with the first row
triangle = [[1]]

# generate the remaining rows of the triangle
for i in range(1, num_rows):
    row = [1]
    for j in range(1, i):
        row.append(triangle[i-1][j-1] + triangle[i-1][j])
    row.append(1)
    triangle.append(row)

# print the triangle
for row in triangle:
    print(" ".join(str(num) for num in row).center(num_rows*3))
Share: