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

Crown Star Pattern

Read summary with AI tools

The crown star pattern prints three points that grow out of a solid band, like a simple drawn crown. The side points grow from the edges and the middle point grows in both directions.

Quick answer: Use a grid 4n + 1 columns wide. On row i of the top part, put a star where j ≤ i (left point), where j > 4n + 1 − i (right point), or within i − 1 columns of the centre column 2n + 1 (middle point). Then print two full rows for the band.

This program is part of the Tricky Patterns collection.

Table of contents8 sections
  1. Output
  2. How the crown 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

Crown Star Pattern output

Output (n = 3):

*     *     *
**   ***   **
*** ***** ***
*************
*************
Crown Star Pattern layout diagram, row by row (n = 3)
How each row of the crown star pattern is built (n = 3). Blue cells are printed characters; grey dots are spaces.

How the crown star pattern works

Each point is a simple shape. The left one is a right triangle against the left edge, the right one is its mirror against the right edge, and the middle one is a small pyramid centred on column 2n + 1. All three grow by one row at a time, so one row loop and one combined condition draw them together. The band underneath is a plain block of stars.

  1. Run the outer loop for i from 1 to n, one row per pass (the three points).
  2. Go through 4 × n + 1 positions in the row. Print stars where j ≤ i or j > 4 × n + 1 − i or (j > 2 × n + 1 − i and j < 2 × n + 1 + i); print spaces everywhere else.
  3. Move to the next line.
  4. Run the outer loop for i from 1 to 2, one row per pass (the band).
  5. Print 4 × n + 1 stars.
  6. Move to the next line.

Key takeaways

  • The crown is three triangles sharing the same rows, plus a solid band.
  • The middle point is centred on column 2n + 1, half way across the 4n + 1 grid.
  • Combining the three tests with || lets one grid loop print all of them.
  • The band is just two rows of 4n + 1 stars.

Crown Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 3;

    // the three points
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 4 * n + 1; j++) {
            if (j <= i || j > 4 * n + 1 - i || (j > 2 * n + 1 - i && j < 2 * n + 1 + i))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    // the band
    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 4 * n + 1; j++)
            printf("*");
        printf("\n");
    }
    return 0;
}

Crown Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 3;

    // the three points
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 4 * n + 1; j++) {
            if (j <= i || j > 4 * n + 1 - i || (j > 2 * n + 1 - i && j < 2 * n + 1 + i))
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }

    // the band
    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 4 * n + 1; j++)
            cout << "*";
        cout << endl;
    }
    return 0;
}

Crown Star Pattern in Java

public class CrownStar {
    public static void main(String[] args) {
        int n = 3;

        // the three points
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 4 * n + 1; j++) {
                if (j <= i || j > 4 * n + 1 - i || (j > 2 * n + 1 - i && j < 2 * n + 1 + i))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }

        // the band
        for (int i = 1; i <= 2; i++) {
            for (int j = 1; j <= 4 * n + 1; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Crown Star Pattern in Python

n = 3

# the three points
for i in range(1, n + 1):
    for j in range(1, 4 * n + 2):
        if j <= i or j > 4 * n + 1 - i or (j > 2 * n + 1 - i and j < 2 * n + 1 + i):
            print("*", end="")
        else:
            print(" ", end="")
    print()

# the band
for i in range(1, 3):
    for j in range(1, 4 * n + 2):
        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 make the points taller?

Raise n. The grid widens to 4n + 1 columns and each point grows to n rows, so the proportions stay the same.

Can I add more points?

Yes. Each extra point is another centred pyramid in the condition. Spread the centres evenly across a wider grid, for example at 2n + 1 and 6n + 1 on a 8n + 1 grid.

Why does the middle point test use a strict comparison?

j > 2n + 1 − i and j < 2n + 1 + i include 2i − 1 columns, the usual pyramid width. Using ≥ and ≤ would make every row two stars too wide.

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.

Latest articles

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.