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
Circle Star Pattern output
Output (n = 4):
* * * * *
* * * *
* * * *
* *
* *
* *
* * * *
* * * *
* * * * *
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.
- Run the outer loop for
ifrom1to2 × n + 1, one row per pass. - Go through
2 × n + 1positions 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. - Move to the next line.
Key takeaways
- The program tests every cell instead of following the curve.
- Comparing squared distances avoids
sqrtand floating-point numbers. - The band
n² − nton² + nkeeps 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.











