The letter P star pattern draws a straight stem with a rounded loop on its top half. The loop is closed by two short bars and a short right-hand side.
Quick answer: Loop over 7 rows and 5 columns. Print the stem in column 1. For the loop, print a bar on row 1 and on the middle row in columns 1 to 4, and the right side in column 5 between them. Stopping the bars one column short rounds the loop.
This program is part of the Star Patterns collection.
Table of contents8 sections
Letter P Star Pattern output
Output (7 rows):
**** * * * * **** * * *

How the letter p star pattern works
The loop is a small hollow box whose corners are left out on the right, which makes it look curved. It only covers the top half, from row 1 to the middle row; below that only the stem is printed. 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 ((i = 1 or i = (n + 1) / 2) and j < n โ 2) or (j = n โ 2 and i > 1 and i < (n + 1) / 2); print spaces everywhere else. - Move to the next line.
Key takeaways
- The P is a stem plus a loop on the top half.
- The bars stop one column short, which rounds the loop.
- The right side runs only between the two bars.
- Add a short diagonal leg under the loop and the P becomes an R.
Letter P 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 || ((i == 1 || i == (n + 1) / 2) && j < n - 2) || (j == n - 2 && i > 1 && i < (n + 1) / 2))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Letter P 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 || ((i == 1 || i == (n + 1) / 2) && j < n - 2) || (j == n - 2 && i > 1 && i < (n + 1) / 2))
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Letter P Star Pattern in Java
public class LetterPStar {
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 || ((i == 1 || i == (n + 1) / 2) && j < n - 2) || (j == n - 2 && i > 1 && i < (n + 1) / 2))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Letter P 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 ((i == 1 or i == (n + 1) // 2) and j < n - 2) or (j == n - 2 and i > 1 and 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 do I turn the P into an R?
Add a leg below the loop: on the rows after the middle, print a star where j โ 1 == i โ 4, which runs diagonally down to the bottom right.
How do I print a B?
Repeat the loop on the bottom half: add bars on the middle and last rows and a right side between them.
Why is the loop not in the middle of the letter?
A printed P has its loop at the top. Keeping it between row 1 and the middle row matches that.






