The hexagon star pattern is an advanced shape pattern in C programming that tests your ability to coordinate multi-phase loop sequencing, space offset alignment, and variable width calculations across composite geometric sections
Introduction
Constructing a balanced hexagon pattern requires decomposing the shape into three distinct segments: an expanding top trapezoid, a uniform rectangular middle block, and a contracting bottom trapezoid
Prerequisites: Proficiency in nested
forloops, standard input/output (printf,scanf), and integer arithmetic. Expected Output (for side size n = 4):
****
******
********
**********
**********
**********
**********
********
******
****
Deconstructing the Pattern Logic
For a side length of n, the total height of the hexagon is 3n - 2 rows
Phase 1: Upper Expanding Trapezoid (i = 1 to n - 1)
Leading Spaces: Decreases each row from n - 1 down to 1 (n - i spaces)
. Asterisks: Expands from n asterisks on the first row in increments of 2, given by n + 2(i - 1).
Phase 2: Central Rectangular Body (i = 1 to n)
Leading Spaces: 0 spaces required
. Asterisks: Maintains a constant max width of 3n - 2 asterisks across n consecutive rows
.
Phase 3: Lower Contracting Trapezoid (i = 1 to n - 1)
Leading Spaces: Increases each row from 1 to n - 1 (i spaces)
. Asterisks: Decreases in increments of 2, given by (3n - 2) - 2i.
| Phase | Row (i) | Leading Spaces | Asterisks | Total Width |
| Upper | 1 | 3 | 4 | 7 |
| Upper | 2 | 2 | 6 | 8 |
| Upper | 3 | 1 | 8 | 9 |
| Middle | 1–4 | 0 | 10 | 10 |
| Lower | 1 | 1 | 8 | 9 |
| Lower | 2 | 2 | 6 | 8 |
| Lower | 3 | 3 | 4 | 7 |
Code Implementation
#include <stdio.h>
int main() {
int n, i, j, space;
printf("Enter the side length of the hexagon: ");
if (scanf("%d", &n) != 1 || n <= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1;
}
// Phase 1: Upper Expanding Trapezoid (n - 1 rows)
for (i = 1; i < n; i++) {
for (space = 1; space <= n - i; space++) {
printf(" ");
}
for (j = 1; j <= n + 2 * (i - 1); j++) {
printf("*");
}
printf("\n");
}
// Phase 2: Central Rectangular Body (n rows)
for (i = 1; i <= n; i++) {
for (j = 1; j <= 3 * n - 2; j++) {
printf("*");
}
printf("\n");
}
// Phase 3: Lower Contracting Trapezoid (n - 1 rows)
for (i = 1; i < n; i++) {
for (space = 1; space <= i; space++) {
printf(" ");
}
for (j = 1; j <= (3 * n - 2) - 2 * i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Code Breakdown
Phase Alignment: Sectioning the loop execution into three distinct outer
forloops avoids complex, error-prone single-loop conditional branching. Maximum Width Calculation: The formula 3n - 2 accurately computes the peak horizontal width at the middle body for any valid side length n.
Line Break Control: Each outer loop iteration terminates with
printf("\n");to print the next row on a new line.
Compiling and Execution
Compile and execute using standard GCC tools
Enter the side length of the hexagon: 4
****
******
********
**********
**********
**********
**********
********
******
****
Common Mistakes & Troubleshooting
Distorted Aspect Ratio: Setting the middle section height to 1 row instead of n rows turns the shape into an elongated diamond rather than a regular hexagon
. Off-by-One Max Width Errors: Using 3n instead of 3n - 2 misaligns the top trapezoid base with the central rectangular body
. Missing Line Breaks: Forgetting
printf("\n");appends all phases onto a single continuous text string.
Complexity Analysis
Time Complexity: O(n^2) because the three consecutive loop phases iterate over 3n - 2 total rows with inner character operations proportional to n.
Space Complexity: O(1) auxiliary memory space, using only scalar integer variables
.
Conclusion
Deconstructing multi-segmented shapes into independent, sequential loop phases simplifies building complex structural layouts in lower-level languages
For all Pattern Programs list click here:
…till the next post, bye-bye & take care