Wednesday, 18 December 2024

Day 44: Python Program To Print Pascal Triangle

 


def print_pascals_triangle(n):

    triangle = [[1]]

    for i in range(1, n):

        previous_row = triangle[-1]

        current_row = [1]  

        for j in range(1, len(previous_row)):

            current_row.append(previous_row[j - 1] + previous_row[j])

        current_row.append(1)  

        triangle.append(current_row)

        for row in triangle:

        print(" " * (n - len(row)), " ".join(map(str, row)))

rows = int(input("Enter the number of rows: "))

print_pascals_triangle(rows)

#source code --> clcoding.com

Code Explanation:

1. Function Definition

def print_pascals_triangle(n):
A function print_pascals_triangle is defined, which takes one argument n (the number of rows to generate).

2. Initializing the Triangle

    triangle = [[1]]
The triangle is initialized with a single row containing the number 1.

3. Building the Triangle

    for i in range(1, n):
        previous_row = triangle[-1]
        current_row = [1]  
A loop runs from 1 to n - 1, generating rows of the triangle:
previous_row: Refers to the last row of the triangle.
current_row: Starts with a 1, as every row in Pascal's Triangle begins with 1.

        for j in range(1, len(previous_row)):
            current_row.append(previous_row[j - 1] + previous_row[j])
For each position in the row (except the first and last), the value is calculated as the sum of the two numbers directly above it from the previous_row.

        current_row.append(1)
        triangle.append(current_row)
The row ends with another 1, and the current_row is appended to the triangle.

4. Printing the Triangle
    for row in triangle:
        print(" " * (n - len(row)), " ".join(map(str, row)))
Each row of the triangle is printed with formatting:
" " * (n - len(row)): Adds spaces to align rows, creating the triangular shape.
" ".join(map(str, row)): Converts numbers in the row to strings and joins them with spaces for proper display.

5. User Input and Function Call
rows = int(input("Enter the number of rows: "))
print_pascals_triangle(rows)
The program prompts the user to input the number of rows and calls the print_pascals_triangle function with the input.

Example Execution
Input:
Enter the number of rows: 5

Output:

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

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (90) AI (37) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (184) C (77) C# (12) C++ (83) Course (67) Coursera (231) Cybersecurity (24) Data Analytics (1) data management (11) Data Science (135) Data Strucures (8) Deep Learning (21) Django (14) Downloads (3) edx (2) Engineering (14) Euron (19) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (5) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (62) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (959) Python Coding Challenge (402) Python Quiz (56) Python Tips (3) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses