Pyramid patterns are the classic way to practise nested loops. Each row prints a few spaces to push the shape to the centre. Then it prints the characters that build the pyramid. Learn how the spaces and characters change from row to row, and you can print almost any triangle.
Pick a pattern to see its output, a row-by-row diagram and the full program in C, C++, Java and Python.
All 7 pyramid pattern programs

Full Pyramid Star Pattern
A centred pyramid of stars where every row has two more stars than the row above.

Inverted Pyramid Star Pattern
The full pyramid turned upside down: the widest row comes first.

Hollow Pyramid Star Pattern
Only the outline of the pyramid is printed; the inside is left empty.

Hollow Inverted Pyramid Star Pattern
The outline of an upside-down pyramid.

Number Pyramid Pattern
A centred pyramid where row i counts from 1 to i.

Palindrome Number Pyramid
Each row counts up to i and back down to 1, so every row reads the…

Diamond Star Pattern
A full pyramid followed by an inverted pyramid, which together make a diamond.
Tips for pyramid patterns
- Work out how many spaces and how many characters each row needs, and write both as a formula in
i. - Draw the pattern on paper with row numbers. The formula is usually
n − ifor spaces and2i − 1for characters. - Get the full pyramid working first; inverted, hollow and diamond versions are small changes to it.
Frequently asked questions
What is a pyramid pattern program?
It is a program that uses nested loops to print a triangle-shaped figure (a pyramid) of stars, numbers or letters. The outer loop handles rows and the inner loops print the spaces and characters for each row.
How many loops do I need to print a pyramid?
Usually one outer loop for the rows and two inner loops: one for the leading spaces and one for the stars or numbers.
How do I make the pyramid bigger?
Change the value of n. All the formulas depend on n, so the whole shape scales automatically.
