The house star pattern, also called the pentagon pattern, draws a simple house: a triangular roof, a flat line under it and a box for the walls. It is three small patterns stacked on top of each other.
Quick answer: Print a hollow pyramid for the roof (rows 1 to n − 1, stars at n − i + 1 and n + i − 1), then one full row of 2n − 1 stars, then a hollow box of n − 2 rows with the last row full.
This program is part of the Star Patterns collection.
Table of contents8 sections
House Star Pattern output
Output (n = 5):
* * * * * * * ********* * * * * *********

How the house star pattern works
Each part is something you may already have printed. The roof is the top of a hollow pyramid without its base. The full row works as both the base of the roof and the ceiling of the house. The box below uses the first and last column for the walls and fills its last row as the floor. All three parts are 2n − 1 columns wide at their widest, so they line up.
- Run the outer loop for
ifrom1ton − 1, one row per pass (roof). - Go through
n + i − 1positions in the row. Print stars wherej = n − i + 1 or j = n + i − 1; print spaces everywhere else. - Move to the next line.
- Run the outer loop for
ifrom1to1, one row per pass (eave). - Print
2 × n − 1stars. - Move to the next line.
- Run the outer loop for
ifrom1ton − 2, one row per pass (walls and floor). - Go through
2 × n − 1positions in the row. Print stars wherej = 1 or j = 2 × n − 1 or i = n − 2; print spaces everywhere else. - Move to the next line.
Key takeaways
- The house is a roof, a full row and a box, printed one after another.
- The roof is a hollow pyramid without its bottom row.
- The full row is shared: it closes the roof and starts the walls.
- Every part is
2n − 1columns wide at its widest, so the walls meet the roof exactly.
House Star Pattern in C
#include <stdio.h>
int main(void) {
int n = 5;
// roof
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n + i - 1; j++) {
if (j == n - i + 1 || j == n + i - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
// eave
for (int i = 1; i <= 1; i++) {
for (int j = 1; j <= 2 * n - 1; j++)
printf("*");
printf("\n");
}
// walls and floor
for (int i = 1; i <= n - 2; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j == 1 || j == 2 * n - 1 || i == n - 2)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}House Star Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
// roof
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n + i - 1; j++) {
if (j == n - i + 1 || j == n + i - 1)
cout << "*";
else
cout << " ";
}
cout << endl;
}
// eave
for (int i = 1; i <= 1; i++) {
for (int j = 1; j <= 2 * n - 1; j++)
cout << "*";
cout << endl;
}
// walls and floor
for (int i = 1; i <= n - 2; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j == 1 || j == 2 * n - 1 || i == n - 2)
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}House Star Pattern in Java
public class HouseStar {
public static void main(String[] args) {
int n = 5;
// roof
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n + i - 1; j++) {
if (j == n - i + 1 || j == n + i - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
// eave
for (int i = 1; i <= 1; i++) {
for (int j = 1; j <= 2 * n - 1; j++)
System.out.print("*");
System.out.println();
}
// walls and floor
for (int i = 1; i <= n - 2; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j == 1 || j == 2 * n - 1 || i == n - 2)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}House Star Pattern in Python
n = 5
# roof
for i in range(1, n):
for j in range(1, n + i):
if j == n - i + 1 or j == n + i - 1:
print("*", end="")
else:
print(" ", end="")
print()
# eave
for i in range(1, 2):
for j in range(1, 2 * n):
print("*", end="")
print()
# walls and floor
for i in range(1, n - 2 + 1):
for j in range(1, 2 * n):
if j == 1 or j == 2 * n - 1 or i == n - 2:
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
How do I add a door?
In the box, also print stars at the two columns either side of the middle on the last two rows, for example j == n − 1 || j == n + 1. Draw it on paper first to pick the columns.
How do I make the walls taller?
Use a separate variable for the box height instead of n − 2. The roof keeps its shape.
Why is the roof hollow but the eave full?
The eave is a straight horizontal line of the drawing, so every position on it is part of the outline. In the roof only the slanted edges are outline.











