New: 208 pattern programs with tested code in C, C++, Java and Python Explore patterns

Letter P Star Pattern

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
  1. Output
  2. How the letter p star pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Letter P Star Pattern output

Output (7 rows):

****
*   *
*   *
****
*
*
*
Letter P Star Pattern layout diagram, row by row (7 rows)
How each row of the letter p star pattern is built (7 rows). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n โˆ’ 2 positions in the row. Print stars where 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 spaces everywhere else.
  3. 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 49 of 49 in Star Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.