Python program to print pyramid pattern using numbers

Given a number N denoting the number of rows. The task is to print the zigzag pattern with N rows as shown in the below examples.

Here's a Python program to print a pyramid pattern using numbers: 

 

# Get the number of rows from the user
num_rows = int(input("Enter the number of rows: "))

# Loop through each row of the pyramid
for i in range(1, num_rows + 1):
    # Print spaces before the numbers to center the pyramid
    print(" " * (num_rows - i), end="")
    # Loop through each number in the row
    for j in range(1, i + 1):
        # Print the number followed by a space
        print(j, end=" ")
    # Move to the next line after printing the row
    print()

In this program, the user is prompted to enter the number of rows they want in the pyramid. Then, a loop is used to iterate through each row of the pyramid.

Before printing the numbers for each row, a certain number of spaces are printed to center the pyramid. The number of spaces printed is determined by the difference between the total number of rows and the current row number.

After printing the spaces, another loop is used to print the numbers for that row. The loop starts at 1 and goes up to the current row number. Each number is printed followed by a space.

Finally, a new line character is printed to move to the next row of the pyramid.

Submit Your Programming Assignment Details