07/04/2023 5:05 am
Topic starter
Notifications
Clear all
How to print Pascal’s triangle?
0
How to print Pascal’s triangle?
Answer
Add a comment
Add a comment
1 Answer
0
07/04/2023 7:44 am
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))
Add a comment
Add a comment
Forum Information
- 14 Forums
- 1,835 Topics
- 5,051 Posts
- 0 Online
- 1,078 Members
Our newest member: Richardnop
Latest Post: loli
Forum Icons:
Forum contains no unread posts
Forum contains unread posts
Topic Icons:
Not Replied
Replied
Active
Hot
Sticky
Unapproved
Solved
Private
Closed