The letter A star pattern draws a capital A, 7 rows tall and 5 stars wide: a rounded top, two straight legs and a crossbar half way down. It is the usual first exercise when you start printing letters with stars.
Quick answer: Loop over 7 rows and 5 columns. Print a star when the cell is part of a stroke: the top bar on row 1 (columns 2 to 4 only, which rounds the corners), the two legs in the first and last column from row 2 down, and the crossbar on the middle row. Every other cell is a space.
This program is part of the Star Patterns collection.
Table of contents8 sections
Letter A Star Pattern output
Output (7 rows):
*** * * * * ***** * * * * * *

How the letter a star pattern works
Draw the A on squared paper and each stroke turns into a condition. The legs are columns 1 and 5 on every row below the top. The crossbar is the whole middle row, row 4. The top is row 1, but leaving out its two corner cells makes the letter look rounded instead of boxy. 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 where(i = 1 and j > 1 and j < n − 2) or i = (n + 1) / 2 or (i > 1 and (j = 1 or j = n − 2)); print spaces everywhere else. - Move to the next line.
Key takeaways
- A letter is a few strokes, and each stroke is one condition.
- Leaving the two top corners empty rounds the top of the A.
- The crossbar sits on row
(n + 1) / 2, the middle row. - The width is
n − 2columns, so the letter keeps its shape if you changento another odd number.
Letter A 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 > 1 && j < n - 2) || i == (n + 1) / 2 || (i > 1 && (j == 1 || j == n - 2)))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Letter A 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 > 1 && j < n - 2) || i == (n + 1) / 2 || (i > 1 && (j == 1 || j == n - 2)))
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Letter A Star Pattern in Java
public class LetterAStar {
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 > 1 && j < n - 2) || i == (n + 1) / 2 || (i > 1 && (j == 1 || j == n - 2)))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Letter A Star Pattern in Python
n = 7
for i in range(1, n + 1):
for j in range(1, n - 2 + 1):
if (i == 1 and j > 1 and j < n - 2) or i == (n + 1) // 2 or (i > 1 and (j == 1 or j == 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
Why is the top row shorter than the others?
Columns 1 and 5 are left empty on row 1, so the top bar is three stars wide. That gap makes the corners look rounded, which is how a printed A usually looks.
How do I print my name in stars?
Print the letters side by side: for each row, print that row of every letter with a couple of spaces between them. Keeping each letter in its own function, such as letterA(row), makes this much easier.
Can I make the A taller?
Yes. Use any odd n, such as 9. The width follows as n − 2 and the crossbar stays on the middle row.










