Question:
Write a program to print the nth line of the Pascal’s Triangle
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def pascal(n): if n == 1: return [1] else: line = [1] previous_line = pascal(n-1) for i in range(len(previous_line)-1): line.append(previous_line[i] + previous_line[i+1]) line += [1] return line j=int(input("Enter line number:")) print(pascal(j)) |
Explanation:
Pascal’s triangle is a triangle where each number is equal to the sum of the one or two numbers above it:
Output:
1 2 |
Enter line number: 9 [1, 8, 28, 56, 70, 56, 28, 8, 1] |
Leave a Reply