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

Hexagon Star Pattern

Read summary with AI tools

The hexagon star pattern draws a six-sided shape: a flat top and bottom of n stars and two slanted sides on the left and right that meet in a point halfway down.

Quick answer: Print the upper half for rows 1 to n: the left edge is at column n − i + 1, the right edge at 2n − 2 + i, and row 1 is filled from column n to the right edge. Then print the same rows from n − 1 back to 1 for the lower half.

This program is part of the Star Patterns collection.

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

Hexagon Star Pattern output

Output (n = 4):

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

How the hexagon star pattern works

The hexagon is two trapeziums joined at their wide side. In the upper half both edges move outwards by one column each row, so the left edge goes n, n − 1, …, 1 and the right edge 2n − 1, 2n, …, 3n − 2. The top row is the flat side, so it is filled between the two edges. Running the same rows backwards draws the lower half.

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

Key takeaways

  • A hexagon is two trapeziums joined at the middle row.
  • The edges move one column outwards per row in the upper half.
  • Only the top and bottom rows are filled; the rest show just the edges.
  • The widest row is 3n − 2 columns.

Hexagon Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;

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

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

Hexagon Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;

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

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

Hexagon Star Pattern in Java

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

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

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

Hexagon Star Pattern in Python

n = 4

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

# lower half
for i in range(n - 1, 0, -1):
    for j in range(1, 2 * n - 2 + i + 1):
        if j == n - i + 1 or j == 2 * n - 2 + i or (i == 1 and j >= n):
            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 does the top have n stars?

Row 1 runs from column n to 2n − 1, which is n columns. Making the flat side the same length as each slanted side gives a regular-looking hexagon.

How do I make it solid?

Change the condition to j >= n − i + 1 (the inner loop already stops at the right edge). Every position between the edges then gets a star.

Why is the middle row printed only once?

The lower half starts at n − 1, so the widest row, row n, belongs only to the upper half.

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.