Solution 1 to 6
Solution 6

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

  1. Start
  2. Input Number of Rows: Read the number of rows n for the pyramid.
  3. Loop for Rows: Iterate from i = 1 to n:
    • Print Leading Spaces: Print n - i spaces.
    • Print Stars: Print 2 * i - 1 stars.
  4. Display Pyramid: Print each row to form the pyramid.
  5. 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