Skip to main content
Skip to content

Loops & Patterns · Intermediate

Star Pattern Program in Hindi Programming

Draw a pyramid and an inverted triangle with nested loops.

The program

Edit anything and press Run. The output below was produced by running this exact code.

Expected output

Press Run to try it

Pyramid:    *   ***  ***** ****************Ulta triangle:* * * * * * * * * * * * * * * 

How it works

Each row of the pyramid is built as a string before it is printed. Row i needs rows - i spaces and then 2 * i - 1 stars, so the shape widens by two stars per row and stays centred.

The inverted triangle counts down instead: the loop starts at rows and uses i = i - 1. Each row prints i copies of "* ".

Pattern programs are good practice for nested loops: the outer loop picks the row, the inner loops decide what goes in it.