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

Letter H Star Pattern

Read summary with AI tools

The letter H star pattern draws two upright posts joined by a bar across the middle. With only three strokes it is the simplest symmetrical letter to print.

Quick answer: Loop over 7 rows and 5 columns. Print a star in the first and last column on every row, and fill the whole middle row, (n + 1) / 2. All other cells are spaces.

This program is part of the Star Patterns collection.

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

Letter H Star Pattern output

Output (7 rows):

*   *
*   *
*   *
*****
*   *
*   *
*   *
Letter H Star Pattern layout diagram, row by row (7 rows)
How each row of the letter h star pattern is built (7 rows). Blue cells are printed characters; grey dots are spaces.

How the letter h star pattern works

Two column tests draw the posts and one row test draws the crossbar. Because the posts run the full height, the crossbar meets them at both ends without any extra work. Every letter on this site is drawn on a grid of rows and columns, and each stroke is one simple test on i and j.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n − 2 positions in the row. Print stars where j = 1 or j = n − 2 or i = (n + 1) / 2; print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • Two posts are the first and last columns.
  • The crossbar is the whole middle row.
  • Three tests joined with || draw the whole letter.
  • The H looks the same upside down, because both posts and the bar are symmetrical.

Letter H Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 7;

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

Letter H Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 7;

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

Letter H Star Pattern in Java

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

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

Letter H Star Pattern in Python

n = 7

for i in range(1, n + 1):
    for j in range(1, n - 2 + 1):
        if j == 1 or j == n - 2 or i == (n + 1) // 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 is it different from the plus sign pattern?

The plus sign has one bar across and one bar down the middle. The H has two posts on the edges instead of one in the centre.

How do I move the crossbar up?

Test a different row, for example i == 3. The crossbar then sits above the middle.

Can I print H and I next to each other?

Yes. For each row, print the H row, two spaces, then the I row: a full row of stars on the top and bottom rows and a single centre star on the others.

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.