Facebook Pixel
Printing a Hollow Square Star Pattern

Can I use multiple inner loops instead of an if statement?

The answer

You could, but it is incredibly messy. Coordinate-based boolean logic is the standard and most readable way to handle hollow shapes.

More FAQs in Printing a Hollow Square Star Pattern

Generate an N x N grid and print a star only if the current row or column is at the absolute boundary (1 or N). Otherwise, print a space.

Because terminal fonts are usually taller than they are wide. A star (*) takes up more vertical space than horizontal space. You can fix this by printing '* ' (with a space) to widen the shape.

This exact boundary logic (checking if row==0 or col==max) is used in graph traversal algorithms (like BFS/DFS in a matrix) to detect when you hit a wall.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.