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

Circle Star Pattern

Read summary with AI tools

The circle star pattern draws the outline of a circle with stars. There is no curve to follow on a text screen, so the program checks each cell of a square grid and keeps the ones that are about one radius away from the centre.

Quick answer: Loop over a (2n + 1) × (2n + 1) grid with the centre at row and column n + 1. For each cell work out the squared distance d = (i − n − 1)² + (j − n − 1)², and print a star when d lies between n² − n and n² + n. Print each cell as two characters so the circle is not squashed.

This program is part of the Tricky Patterns collection.

Table of contents8 sections
  1. Output
  2. How the circle 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

Circle Star Pattern output

Output (n = 4):

    * * * * *
  * *       * *
* *           * *
*               *
*               *
*               *
* *           * *
  * *       * *
    * * * * *
Circle Star Pattern layout diagram, row by row (n = 4)
How each row of the circle star pattern is built (n = 4). Blue cells are printed characters; grey dots are spaces.

How the circle star pattern works

A circle is every point at distance n from the centre. On a grid hardly any cell sits exactly at that distance, so the program accepts a band around it. Comparing squared distances avoids square roots: n² − n ≤ d ≤ n² + n keeps the cells that are within about half a cell of the circle. The second trick is the width: a character on screen is about twice as tall as it is wide, so each cell is printed as "* " or two spaces to make it roughly square.

  1. Run the outer loop for i from 1 to 2 × n + 1, one row per pass.
  2. Go through 2 × n + 1 positions in the row. Print stars where (i − n − 1) × (i − n − 1) + (j − n − 1) × (j − n − 1) ≥ n × n − n and (i − n − 1) × (i − n − 1) + (j − n − 1) × (j − n − 1) ≤ n × n + n; print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • The program tests every cell instead of following the curve.
  • Comparing squared distances avoids sqrt and floating-point numbers.
  • The band n² − n to n² + n keeps the outline one cell thick.
  • Printing two characters per cell corrects the tall shape of text characters.

Circle Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;

    for (int i = 1; i <= 2 * n + 1; i++) {
        for (int j = 1; j <= 2 * n + 1; j++) {
            if ((i - n - 1) * (i - n - 1) + (j - n - 1) * (j - n - 1) >= n * n - n && (i - n - 1) * (i - n - 1) + (j - n - 1) * (j - n - 1) <= n * n + n)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
    return 0;
}

Circle Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;

    for (int i = 1; i <= 2 * n + 1; i++) {
        for (int j = 1; j <= 2 * n + 1; j++) {
            if ((i - n - 1) * (i - n - 1) + (j - n - 1) * (j - n - 1) >= n * n - n && (i - n - 1) * (i - n - 1) + (j - n - 1) * (j - n - 1) <= n * n + n)
                cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }
    return 0;
}

Circle Star Pattern in Java

public class CircleStar {
    public static void main(String[] args) {
        int n = 4;

        for (int i = 1; i <= 2 * n + 1; i++) {
            for (int j = 1; j <= 2 * n + 1; j++) {
                if ((i - n - 1) * (i - n - 1) + (j - n - 1) * (j - n - 1) >= n * n - n && (i - n - 1) * (i - n - 1) + (j - n - 1) * (j - n - 1) <= n * n + n)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
}

Circle Star Pattern in Python

n = 4

for i in range(1, 2 * n + 2):
    for j in range(1, 2 * n + 2):
        if (i - n - 1) * (i - n - 1) + (j - n - 1) * (j - n - 1) >= n * n - n and (i - n - 1) * (i - n - 1) + (j - n - 1) * (j - n - 1) <= n * n + n:
            print("* ", end="")
        else:
            print("  ", end="")
    print()

Time and space complexity

The program checks each of the (2n + 1)² cells once, so it runs in O(n²) time with O(1) extra memory.

Frequently asked questions

Why does the circle look a little uneven?

Because the grid is coarse: a circle of radius 4 only has a handful of cells near its edge. Larger values of n, such as 8 or 10, give a smoother outline.

How do I print a filled circle?

Use one condition, d ≤ n² + n, instead of the band. Every cell inside the circle then gets a star.

Why print “* ” instead of “*”?

Characters are about twice as tall as they are wide. With one character per cell the circle comes out as a tall oval; two characters per cell make the cells close to square, so it looks round.

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.