The octagon star pattern draws an eight-sided outline inside a square grid: four straight sides on the top, bottom, left and right, and four short diagonal sides cutting off the corners.
Quick answer: Use a (3n − 2) × (3n − 2) grid. Print stars on the four straight sides, which cover the middle n cells of the first and last row and column, and on the four corner diagonals: i + j = n + 1, j − i = 2n − 2, i − j = 2n − 2 and i + j = 5n − 3.
This program is part of the Star Patterns collection.
Table of contents8 sections
Octagon Star Pattern output
Output (n = 3):
*** * * * * * * * * * * ***

How the octagon star pattern works
Split the grid into three bands of n cells, overlapping by one where they meet. The straight sides run along the middle band of each edge. Each corner is cut by a diagonal line, and on a grid a diagonal is a fixed sum or difference of the row and column. The extra checks i < n and i > 2n − 1 stop the diagonals from running into the middle of the shape.
- Run the outer loop for
ifrom1to3 × n − 2, one row per pass. - Go through
3 × n − 2positions in the row. Print stars where((i = 1 or i = 3 × n − 2) and j ≥ n and j ≤ 2 × n − 1) or ((j = 1 or j = 3 × n − 2) and i ≥ n and i ≤ 2 × n − 1) or (i < n and (i + j = n + 1 or j − i = 2 × n − 2)) or (i > 2 × n − 1 and (i − j = 2 × n − 2 or i + j = 5 × n − 3)); print spaces everywhere else. - Move to the next line.
Key takeaways
- The octagon is four straight sides and four diagonal corners.
- Diagonals on a grid are fixed sums or differences of
iandj. - Limiting each diagonal to its corner rows keeps it from crossing the middle.
- With
n = 3every side is 3 stars long, which gives a regular look.
Octagon Star Pattern in C
#include <stdio.h>
int main(void) {
int n = 3;
for (int i = 1; i <= 3 * n - 2; i++) {
for (int j = 1; j <= 3 * n - 2; j++) {
if (((i == 1 || i == 3 * n - 2) && j >= n && j <= 2 * n - 1) || ((j == 1 || j == 3 * n - 2) && i >= n && i <= 2 * n - 1) || (i < n && (i + j == n + 1 || j - i == 2 * n - 2)) || (i > 2 * n - 1 && (i - j == 2 * n - 2 || i + j == 5 * n - 3)))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Octagon Star Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 3;
for (int i = 1; i <= 3 * n - 2; i++) {
for (int j = 1; j <= 3 * n - 2; j++) {
if (((i == 1 || i == 3 * n - 2) && j >= n && j <= 2 * n - 1) || ((j == 1 || j == 3 * n - 2) && i >= n && i <= 2 * n - 1) || (i < n && (i + j == n + 1 || j - i == 2 * n - 2)) || (i > 2 * n - 1 && (i - j == 2 * n - 2 || i + j == 5 * n - 3)))
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Octagon Star Pattern in Java
public class OctagonStar {
public static void main(String[] args) {
int n = 3;
for (int i = 1; i <= 3 * n - 2; i++) {
for (int j = 1; j <= 3 * n - 2; j++) {
if (((i == 1 || i == 3 * n - 2) && j >= n && j <= 2 * n - 1) || ((j == 1 || j == 3 * n - 2) && i >= n && i <= 2 * n - 1) || (i < n && (i + j == n + 1 || j - i == 2 * n - 2)) || (i > 2 * n - 1 && (i - j == 2 * n - 2 || i + j == 5 * n - 3)))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Octagon Star Pattern in Python
n = 3
for i in range(1, 3 * n - 2 + 1):
for j in range(1, 3 * n - 2 + 1):
if ((i == 1 or i == 3 * n - 2) and j >= n and j <= 2 * n - 1) or ((j == 1 or j == 3 * n - 2) and i >= n and i <= 2 * n - 1) or (i < n and (i + j == n + 1 or j - i == 2 * n - 2)) or (i > 2 * n - 1 and (i - j == 2 * n - 2 or i + j == 5 * n - 3)):
print("*", end="")
else:
print(" ", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
Why is the grid 3n − 2 wide?
Each straight side is n cells and each corner diagonal spans n − 1 more cells. Two corners plus one side make n + 2(n − 1) = 3n − 2.
How do I find the four diagonal equations?
Mark the two end cells of a diagonal on squared paper and add or subtract their row and column numbers. If the sum stays the same along the line, use the sum; if the difference stays the same, use the difference.
How is it related to the diamond inside a square?
Both patterns use diagonal lines on a square grid. The diamond inside a square draws long diagonals from edge to edge; the octagon only uses short ones in the corners.











