codescracker
python

Python Program to Print Pascal's Triangle



« Previous Program Next Program »

This article is created to cover some programs in Python that print Pascal's Triangle in different-different ways. Here are the list of programs covered in this article:

But before going through these programs, understanding about Pascal's triangle is compulsory. Therefore let's discuss about it.

How Pascal's Triangle Looks Like ?

Pascal's Triangle looks like an equilateral triangle where each row gets created in following ways:

Here is a Pascal's Triangle of 4 rows:

   1
  1 1
 1 2 1
1 3 3 1

To learn more about it, refer to Pascal's Triangle to get every required things about Pascal's Triangle such as structure of Pascal's triangle, how Pascal's triangle gets expanded, and many more things. But for now, the figure given below tells a lot about it:

pascal triangle python

Print Pascal's Triangle in Python

The question is, write a Python program that prints Pascal's triangle of 5 rows. The program given below is the answer to this question. This program is created using user-based or self-created code. That is, this program doesn't uses any function or formula to do the task.

arr = []
arrTemp = []
arr.insert(0, 1)
arr.insert(1, 1)
i = 1
j = 0
for row in range(5):
  for col in range(4, row, -1):
    print(end=" ")
  for col in range(row+1):
    if row==0:
      print(end="1")
    else:
      if col==0 or col==row:
        print(end="1 ")
      else:
        arrTemp.insert(i, (arr[j]+arr[j+1]))
        print(arrTemp[i], end=" ")
        i = i+1
        j = j+1
  print()
  arrTemp.insert(i, 1)
  if row>1:
    i = 1
    j = 0
    arr[j] = 1
    for j in range(1, row+1):
      arr.insert(j, arrTemp[i])
      i = 1
      j = 0

The snapshot given below shows the sample output produced by this Python program:

python print pascal triangle

Print Pascal's Triangle using Function and Formula

This is the program that takes help of function and formula to construct and print Pascal's triangle. To understand about formula while constructing the element of Pascal's triangle, must refer to the article provided above, that is a separate tutorial on Pascal's triangle.

def fact(num):
  f = 1
  for i in range(1, num+1):
    f = f*i
  return f

for i in range(5):
  for col in range(4, i, -1):
    print(end=" ")
  for col in range(i+1):
    val = int(fact(i)/(fact(col)*fact(i-col)))
    print(val, end=" ")
  print()

This program produces the same output as of previous program.

Print Pascal's Triangle upto n Rows

The question is, write a Python program that prints Pascal's triangle upto n rows. The value of n must be entered by user. Here is its answer:

import math

print(end="Enter the Value of n: ")
n = int(input())
for i in range(n):
  for col in range(n-1, i, -1):
    print(end=" ")
  for col in range(i+1):
    val = int(math.factorial(i)/(math.factorial(col)*math.factorial(i-col)))
    print(val, end=" ")
  print()

Here is its sample run with user input 7 as number of rows:

print pascal triangle python

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker