The letter B star pattern draws a straight stem with two closed loops, one on the top half and one on the bottom half. It is the letter P with a second loop added underneath.
Quick answer: Loop over 7 rows and 5 columns. Print the stem in column 1 on every row. Print three bars in columns 1 to 4 on row 1, the middle row and the last row, and the right side in column 5 on the rows between the bars. Stopping the bars one column short rounds both loops.
This program is part of the Star Patterns collection.
Table of contents8 sections
Letter B Star Pattern output
Output (7 rows):
**** * * * * **** * * * * ****

How the letter b star pattern works
A B is two small boxes stacked on top of each other that share the middle bar. Each box has its right-hand corners left empty, so the bars stop at column 4 while the right side sits in column 5. That gap is what makes the loops look curved instead of square. 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 or i = (n + 1) / 2 or i = n) and j < n − 2) or (j = n − 2 and i ≠ 1 and i ≠ (n + 1) / 2 and i ≠ n) or j = 1; print spaces everywhere else. - Move to the next line.
Key takeaways
- A B is a stem plus two loops that share the middle bar.
- The three bars stop one column short, which rounds the loops.
- The right side is printed on every row that is not a bar row.
- Remove the bottom loop and the letter becomes a P.
Letter B 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 || i == (n + 1) / 2 || i == n) && j < n - 2) || (j == n - 2 && i != 1 && i != (n + 1) / 2 && i != n) || j == 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Letter B 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 || i == (n + 1) / 2 || i == n) && j < n - 2) || (j == n - 2 && i != 1 && i != (n + 1) / 2 && i != n) || j == 1)
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Letter B Star Pattern in Java
public class LetterBStar {
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 || i == (n + 1) / 2 || i == n) && j < n - 2) || (j == n - 2 && i != 1 && i != (n + 1) / 2 && i != n) || j == 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Letter B 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 i == (n + 1) // 2 or i == n) and j < n - 2) or (j == n - 2 and i != 1 and i != (n + 1) // 2 and i != n) or j == 1:
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 the B different from the letter D?
The letter D has one tall loop from top to bottom. The B splits that space into two loops with a bar across the middle.
Why do the bars stop at column 4?
The empty cell at the end of each bar is the corner of a loop. Leaving it blank makes the loop look round, the way a printed B looks.
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.






