Python program to print pyramid pattern

Write to program to print the pyramid pattern formed of stars 

Here's a Python program that will print a pyramid pattern: 

 

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

for i in range(1, num_rows+1):
    for j in range(1, num_rows-i+1):
        print(" ", end="")
    for k in range(1, i*2):
        print("*", end="")
    print()

Here's how the program works:

  1. The user is prompted to enter the number of rows for the pyramid.

  2. A for loop is used to iterate through each row of the pyramid. The loop variable i represents the current row number.

  3. Two nested for loops are used to print the spaces and asterisks for each row. The first nested loop (j) prints the required number of spaces before each row. The number of spaces decreases as the rows increase.

  4. The second nested loop (k) prints the required number of asterisks for each row. The number of asterisks increases as the rows increase.

  5. The print() function is used to move to the next line after each row is printed.

This program will output a pyramid pattern with the number of rows specified by the user. The pyramid will be centered and will have the number of asterisks increasing by 2 for each row.

Submit Your Programming Assignment Details