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
Hexagon Star Pattern output
Output (n = 4):
**** * * * * * * * * * * ****

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.
- Run the outer loop for
ifrom1ton, one row per pass (upper half). - Go through
2 × n − 2 + ipositions in the row. Print stars wherej = n − i + 1 or j = 2 × n − 2 + i or (i = 1 and j ≥ n); print spaces everywhere else. - Move to the next line.
- Run the outer loop for
ifromn − 1down to1, one row per pass (lower half). - Go through
2 × n − 2 + ipositions in the row. Print stars wherej = n − i + 1 or j = 2 × n − 2 + i or (i = 1 and j ≥ n); print spaces everywhere else. - 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 − 2columns.
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.











