Instead of both loops running up to N, the outer loop runs up to the 'height' variable, and the inner loop runs up to the 'width' variable.
How to Print a Square Star Pattern
How do I print a rectangle instead of a square?
The answer
More FAQs in How to Print a Square Star Pattern
In a square pattern, the number of columns printed is constant and does not depend on which row is currently being printed.
The outer loop runs N times, and the inner loop runs N times. Therefore, the time complexity is exactly O(N^2).
Using 'print' keeps the output on the same horizontal line. Using 'println' would output each star on a completely new line, ruining the grid.
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.