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
Letter H Star Pattern output
Output (7 rows):
* * * * * * ***** * * * * * *

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.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
n − 2positions in the row. Print stars wherej = 1 or j = n − 2 or i = (n + 1) / 2; print spaces everywhere else. - 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.









