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:
The user is prompted to enter the number of rows for the pyramid.
A for
loop is used to iterate through each row of the pyramid. The loop variable i
represents the current row number.
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.
The second nested loop (k
) prints the required number of asterisks for each row. The number of asterisks increases as the rows increase.
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.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.