New: 203 pattern programs with tested code in C, C++, Java and Python Explore patterns

Concentric Squares Star Pattern

Read summary with AI tools

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
  1. Output
  2. How the concentric squares star pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Concentric Squares Star Pattern output

Output (n = 4 (7 × 7)):

*******
*     *
* *** *
* * * *
* *** *
*     *
*******
Concentric Squares Star Pattern layout diagram, row by row (n = 4 (7 × 7))
How each row of the concentric squares star pattern is built (n = 4 (7 × 7)). Blue cells are printed characters; grey dots are spaces.

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.

  1. Set the width w = 2n − 1.
  2. For each cell, take d as the smallest of i − 1, j − 1, w − i and w − j.
  3. Print a star if d is even, otherwise a space.
  4. 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 d or n − d instead 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.

The concentric square number pattern prints a number based on the same distance. Here the distance only decides star or space.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.