The concentric squares star pattern draws squares inside squares: a star frame, a gap, a smaller star frame, and so on to the middle. It is a star version of the concentric square number pattern.
Quick answer: For every cell work out its distance to the nearest edge: the smallest of i − 1, j − 1, w − i and w − j. Print a star when that distance is even and a space when it is odd. Rings at distance 0, 2, 4 become star frames.
This program is part of the Star Patterns collection.
Table of contents8 sections
Concentric Squares Star Pattern output
Output (n = 4 (7 × 7)):
******* * * * *** * * * * * * *** * * * *******

How the concentric squares star pattern works
Every square ring is the set of cells at the same distance from the outer edge: the border is distance 0, the next ring in is distance 1, and so on. Once you can compute that distance, the picture is one rule: even rings are stars, odd rings are gaps. The same distance is what the number version prints directly.
- Set the width
w = 2n − 1. - For each cell, take
das the smallest ofi − 1,j − 1,w − iandw − j. - Print a star if
dis even, otherwise a space. - Move to the next line after each row.
Key takeaways
- Each ring is the set of cells at the same distance from the edge.
- The distance is the smallest of the four gaps to the edges.
- Even distances are star frames; odd distances are the gaps between them.
- Print
dorn − dinstead of a star to get number rings.
Concentric Squares Star Pattern in C
#include <stdio.h>
int main(void) {
int n = 4;
int w = 2 * n - 1;
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= w; j++) {
int d = i - 1; /* distance to the nearest edge */
if (j - 1 < d) d = j - 1;
if (w - i < d) d = w - i;
if (w - j < d) d = w - j;
printf(d % 2 == 0 ? "*" : " ");
}
printf("\n");
}
return 0;
}Concentric Squares Star Pattern in C++
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n = 4;
int w = 2 * n - 1;
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= w; j++) {
int d = min(min(i - 1, j - 1), min(w - i, w - j)); // distance to the nearest edge
cout << (d % 2 == 0 ? "*" : " ");
}
cout << endl;
}
return 0;
}Concentric Squares Star Pattern in Java
public class ConcentricSquaresStar {
public static void main(String[] args) {
int n = 4;
int w = 2 * n - 1;
for (int i = 1; i <= w; i++) {
for (int j = 1; j <= w; j++) {
int d = Math.min(Math.min(i - 1, j - 1), Math.min(w - i, w - j)); // distance to the nearest edge
System.out.print(d % 2 == 0 ? "*" : " ");
}
System.out.println();
}
}
}Concentric Squares Star Pattern in Python
n = 4
w = 2 * n - 1
for i in range(1, w + 1):
for j in range(1, w + 1):
d = min(i - 1, j - 1, w - i, w - j) # distance to the nearest edge
print("*" if d % 2 == 0 else " ", end="")
print()Time and space complexity
The program visits each of the (2n − 1)² cells once, so it runs in O(n²) time with O(1) extra memory.
Frequently asked questions
Why is the middle cell a space?
With n = 4 the middle is at distance 3, which is odd. For n = 3 or n = 5 the middle distance is even and the centre becomes a star.
How do I get thicker frames?
Use (d / 2) % 2 == 0 instead of d % 2 == 0. Each frame and each gap is then two cells wide.
How is it related to the number version?
The concentric square number pattern prints a number based on the same distance. Here the distance only decides star or space.











