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

House Star Pattern

Read summary with AI tools

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

House Star Pattern output

Output (n = 5):

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

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.

  1. Run the outer loop for i from 1 to n − 1, one row per pass (roof).
  2. Go through n + i − 1 positions in the row. Print stars where j = n − i + 1 or j = n + i − 1; print spaces everywhere else.
  3. Move to the next line.
  4. Run the outer loop for i from 1 to 1, one row per pass (eave).
  5. Print 2 × n − 1 stars.
  6. Move to the next line.
  7. Run the outer loop for i from 1 to n − 2, one row per pass (walls and floor).
  8. Go through 2 × n − 1 positions in the row. Print stars where j = 1 or j = 2 × n − 1 or i = n − 2; print spaces everywhere else.
  9. 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 − 1 columns 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.

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.