Open In App

Python program to print Pascal’s Triangle

Last Updated : 28 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Pascal’s triangle is a pattern of the triangle which is based on nCr, below is the pictorial representation of Pascal’s triangle.

Example:

Input: N = 5
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Method 1: Using nCr formula i.e. n!/(n-r)!r!

After using nCr formula, the pictorial representation becomes:

          0C0
1C0 1C1
2C0 2C1 2C2
3C0 3C1 3C2 3C3

Algorithm:

  • Take a number of rows to be printed, lets assume it to be n
  • Make outer iteration i from 0 to n times to print the rows.
  • Make inner iteration for j from 0 to (N – 1).
  • Print single blank space ” “.
  • Close inner loop (j loop) //its needed for left spacing.
  • Make inner iteration for j from 0 to i.
  • Print nCr of i and j.
  • Close inner loop.
  • Print newline character (\n) after each inner iteration.

Implementation:

Python3
# Print Pascal's Triangle in Python
from math import factorial

# input n
n = 5
for i in range(n):
    for j in range(n-i+1):

        # for left spacing
        print(end=" ")

    for j in range(i+1):

        # nCr = n!/((n-r)!*r!)
        print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")

    # for new line
    print()

Output
      1 
     1 1 
    1 2 1 
   1 3 3 1 
  1 4 6 4 1 

Time complexity: O(N2)
Auxiliary space: O(1)

Method 2: We can optimize the above code by the following concept of a Binomial Coefficient, the i’th entry in a line number line is Binomial Coefficient C(line, i) and all lines start with value 1. The idea is to calculate C(line, i) using C(line, i-1).

C(line, i) = C(line, i-1) * (line - i + 1) / i

Implementations:

Python3
# Print Pascal's Triangle in Python

# input n
n = 5

for i in range(1, n+1):
    for j in range(0, n-i+1):
        print(' ', end='')

    # first element is always 1
    C = 1
    for j in range(1, i+1):

        # first value in a line is always 1
        print(' ', C, sep='', end='')

        # using Binomial Coefficient
        C = C * (i - j) // j
    print()

Output
      1
     1 1
    1 2 1
   1 3 3 1
  1 4 6 4 1

Time complexity: O(N2)
Auxiliary Space: O(1)

Method 3: The code prints Pascal’s Triangle up to the 6th row. It iterates through each row and calculates each value using the binomial coefficient formula, which is

\frac{n!}{k! (n-k)!}

where n is the row number and k is the position in the row.

Implementation:

Python3
# Print Pascal's Triangle in Python

# input n
n = 6

# iterate up to n
for i in range(n):
    # adjust space
    print(' '*(n-i), end='')

    # compute each value in the row
    coef = 1
    for j in range(0, i + 1):
        print(coef, end=' ')
        coef = coef * (i - j) // (j + 1)
    print()

Output
      1 
     1 1 
    1 2 1 
   1 3 3 1 
  1 4 6 4 1 
 1 5 10 10 5 1 

Time Complexity: O(N^2)
Auxiliary Space: O(N^2)



Previous Article
Next Article

Similar Reads

Find the Nth row in Pascal's Triangle
Given a non-negative integer N, the task is to find the Nth row of Pascal's Triangle. Note: The row index starts from 0. Pascal's Triangle: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 Examples: Input: N = 3 Output: 1, 3, 3, 1 Explanation: The elements in the 3rd row are 1 3 3 1. Input: N = 0 Output: 1 Naive Approach: The simplest approach to solve the problem is
9 min read
Check if Pascal's Triangle is possible with a complete layer by using numbers upto N
Given a number N, the task is to determine if it is possible to make Pascal's triangle with a complete layer by using total number N integer if possible print Yes otherwise print No. Note: Pascal’s triangle is a triangular array of the binomial coefficients. Following are the first 6 rows of Pascal’s Triangle. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 1
4 min read
Pascal's Triangle
Pascal's triangle is a triangular array of binomial coefficients. Write a function that takes an integer value N as input and prints the first N lines of Pascal's triangle. Example: The below image shows the Pascal's Triangle for N=6 Recommended PracticePascal TriangleTry It!Pascal's Triangle using Binomial Coefficient:The number of entries in ever
15+ min read
Program to print a Hollow Triangle inside a Triangle
Given a number N(? 8), the task is to print a Hollow Triangle inside a Triangle pattern.Example: Input: N = 9 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Approach: Let i be the index for rows and j be the index for columns. Then: For sides of outer triangle: If the index of column(j) is equals to (N - i + 1) or (
7 min read
Python - Convert Snake case to Pascal case
Sometimes, while working with Python Strings, we have problem in which we need to perform a case conversion of String. This a very common problem. This can have application in many domains such as web development. Lets discuss certain ways in which this task can be performed. Input : geeks_for_geeks Output : GeeksforGeeks Input : left_index Output
7 min read
Python Program to Print Alphabetic Triangle Pattern
In this article, we examine a recursive method for printing character patterns in Python. Nesting loops are used in the conventional iterative approach to regulating character spacing and printing. We have provided the answer using both methods. This method prints an alphabet pattern in the form of a pyramid. It uses nested loops to print the space
4 min read
Program to print Reverse Floyd's triangle
Floyd’s triangle is a triangle with first natural numbers. Task is to print reverse of Floyd’s triangle.Examples: Input : 4 Output : 10 9 8 7 6 5 4 3 2 1 Input : 5 Output : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 C/C++ Code // CPP program to print reverse of // floyd's triangle #include <bits/stdc++.h> using namespace std; void printReverseFloyd(
3 min read
Program to print hollow Triangle pattern
Given the number of lines as N, the task is to form the given hollow Triangle pattern.Examples: Input: N = 6 Output: ************ ***** ***** **** **** *** *** ** ** * * Approach: Input number of rows to print from the user as N.To iterate through rows run an outer loop from number of rows till it is greater than 1. The loop structure should look l
6 min read
Program to print modified Binary triangle pattern
Given an integer N, the task is to print the modified binary tree pattern. In Modified Binary Triangle Pattern, the first and last element of a Nth row are 1 and the middle (N - 2) elements are 0. Examples: Input: N = 6 Output: 1 11 101 1001 10001 100001Input: N = 3 Output: 1 11 101 Approach: From the above examples, it can be observed that, for ea
5 min read
Program to Print Floyd's Triangle
Floyd's triangle is a triangle with first natural numbers. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Recommended PracticeFloyd\'s triangleTry It! Following program prints Floyd's triangle with n lines. C/C++ Code #include <bits/stdc++.h> using namespace std; void printFloydTriangle(int n) { int i, j, val = 1; for (i = 1; i <= n; i++) { for (j =
3 min read
three90RightbarBannerImg