Learn loop control flow and nested loops by building visual star geometry.
16 algorithms4 problems
16 algorithms. Each one stands alone.
Star patterns print shapes such as triangles, pyramids and diamonds out of asterisks. They are the classic first exercise in nested loops: each shape turns a picture into a rule about rows and columns. Most pages open at n = 5; the heart, tree and arrow have one fixed size, and the hollow pyramid, rhombus and hollow diamond open smaller.
Every pattern is a grid walked row by row: the outer loop picks row i, the inner loop visits each column j, and one condition on i and j decides star or space. The shapes differ only in that condition: triangles compare j with a count that changes with i, pyramids add leading spaces, the diamond and butterfly turn i into a level that rises and falls, and the hollow square tests the edges.
Follow the order above; each page adds one idea. When a shape comes out wrong, check the first and last rows by hand: most bugs are an off-by-one such as j < i where j <= i was meant. Every page runs in O(n²) time.