The letter T star pattern prints a full bar across the top and a single column of stars down the middle. Only two strokes, but it is a good check that you can find the centre column.
Quick answer: Loop over 7 rows and 5 columns. Print a star on the whole first row and in the centre column, column 3, on every row. Everything else is a space, so the rows below the bar print two spaces and then a single star.
This program is part of the Star Patterns collection.
Table of contents8 sections
Letter T Star Pattern output
Output (7 rows):
***** * * * * * *

How the letter t star pattern works
A letter 5 columns wide has its centre in column 3, which is (n โ 1) / 2 when n = 7. The top bar is a row test and the stem is a column test, and together they make the T. 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 wherei = 1 or j = (n โ 1) / 2; print spaces everywhere else. - Move to the next line.
Key takeaways
- The T is a top bar plus a centre column.
- An odd width gives a single centre column.
- Rows below the top print two spaces and then the star.
- Move the bar to the bottom row and you get an upside-down T.
Letter T 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 (i == 1 || j == (n - 1) / 2)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Letter T 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 (i == 1 || j == (n - 1) / 2)
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Letter T Star Pattern in Java
public class LetterTStar {
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 (i == 1 || j == (n - 1) / 2)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Letter T Star Pattern in Python
n = 7
for i in range(1, n + 1):
for j in range(1, n - 2 + 1):
if i == 1 or j == (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 do I print the letter I?
Add a bottom bar to the T: i == 1 || i == n || j == 3. The I has bars at both ends.
What happens with an even width?
There is no single centre column, so the stem has to be two columns wide or sit off-centre. Keep the width odd.
How do I let the user choose the size?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.






