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

Octagon Star Pattern

Read summary with AI tools

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
  1. Output
  2. How the octagon 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

Octagon Star Pattern output

Output (n = 3):

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

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.

  1. Run the outer loop for i from 1 to 3 × n − 2, one row per pass.
  2. Go through 3 × n − 2 positions 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.
  3. 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 i and j.
  • Limiting each diagonal to its corner rows keeps it from crossing the middle.
  • With n = 3 every 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.

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.

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.