How to use the Pattern Programs Generator
- 1. Pick a patternTriangles, pyramids, diamond, hollow square, Floyd's and Pascal's triangles.
- 2. Set rows and languageChoose 1 to 20 rows and Python, C, Java or JavaScript.
- 3. Copy the programGet the code and the output it prints, ready to run.
The idea behind every pattern
Pattern programs train nested loops. The outer loop picks the row; the inner loops print what goes in that row. The trick is to write the count for row i as a formula in i and n:
- Right triangle: row i has i stars.
- Pyramid: row i has n − i spaces, then i stars (each star followed by a space).
- Inverted shapes: run the outer loop from n down to 1.
- Diamond: a pyramid, then an inverted pyramid with one row fewer.
- Hollow square: print a star only on the first and last row and column.
Once you can write that formula on paper, the code in any language is just a translation.
Worked example: a 5-row pyramid
With 5 rows, row 1 has 4 spaces and 1 star, row 2 has 3 spaces and 2 stars, and so on until row 5 has no spaces and 5 stars. The output is:
*
* *
* * *
* * * *
* * * * *The Python program the tool gives is two lines inside the loop: print(" " * (n - i) + "* " * i). The C and Java versions use two inner for loops, one for spaces and one for stars.
Number patterns follow the same plan. Floyd's triangle keeps a counter that goes up after every print, so 5 rows end at 15. Pascal's triangle uses c = c × (i − j) ÷ (j + 1) to get each value from the one before it, without working out factorials. Every program this tool gives was compiled and run to check that it prints exactly the preview shown.
Frequently asked questions
Why are there spaces after each star?
A space after each star makes pyramids look even and is how most textbooks print them. Remove the space in the code if your teacher wants stars without gaps.
Which class name does the Java code use?
Main. Save the file as Main.java, or change the class name to match your file name.
Can I get more than 20 rows?
Change the value of n in the code. The tool stops at 20 so the preview fits on a phone screen.
Is the generator free?
Yes, free for students. Your first program asks for a name and WhatsApp number once, and after that every Uncodemy tool opens directly.