Display a Pyramid of Stars
This program displays a pyramid pattern made of stars *
, The number of rows in the pyramid is specified by the user. The program calculates the correct spacing and number of stars for each row to form a pyramid shape.
Code Breakdown
Algorithm
- Start
- Input Number of Rows: Read the number of rows
n
for the pyramid. - Loop for Rows: Iterate from
i = 1
ton
:- Print Leading Spaces: Print
n - i
spaces. - Print Stars: Print
2 * i - 1
stars.
- Print Leading Spaces: Print
- Display Pyramid: Print each row to form the pyramid.
- End
Code Explanation
Example Flowchart
Start
|
V
Input number of rows (n)
|
V
Initialize i = 1
|
V
i <= n? (Check row number)
/ \
/ \
Yes No
/ \
V V
Print newline for each row End
|
V
Initialize j = 0 (for leading spaces)
|
V
Is j < (n - i)? (Check space count)
/ \
/ \
Yes No
| |
V V
Print space Initialize j = 0 (for stars)
| |
V V
| Is j < (2 * i - 1)? (Check star count)
| / \
| / \
| Yes No
| / \
| V V
| Print star Initialize j = 0 (for next row)
| | |
| V V
| | Increment i
| | |
| V V
| V Repeat (Check star count)
| |
V V
End End