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

Heart Star Pattern

Read summary with AI tools

The heart star pattern prints a heart with two rounded lobes on top and a point at the bottom. It looks hard, but it is two small pyramids side by side sitting on one large inverted pyramid.

Quick answer: Split the heart into two parts. The top n rows draw the lobes: on row i print n − i spaces, 2i + 1 stars, a gap of 2(n − i) + 1 spaces and 2i + 1 stars again. The rest is an inverted pyramid that starts at full width, 4n + 3 stars, and loses two stars per row.

This program is part of the Tricky Patterns collection.

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

Heart Star Pattern output

Output (n = 3):

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

How the heart star pattern works

Draw the heart on squared paper first and the two parts stand out. The lobes are two pyramids printed on the same rows, with a gap between them that shrinks by two each row until it is a single space. The point below them is the familiar inverted pyramid. The only careful step is making the widths meet: on the last lobe row the two lobes and the one-space gap together are 4n + 3 characters wide, so the inverted pyramid starts at exactly that width.

  1. Run the outer loop for i from 1 to n, one row per pass (the two round lobes).
  2. Print n − i spaces.
  3. Print 2 × i + 1 stars.
  4. Print 2 × (n − i) + 1 spaces.
  5. Print 2 × i + 1 stars.
  6. Move to the next line.
  7. Run the outer loop for i from 1 to 2 × n + 2, one row per pass (the point: an inverted pyramid).
  8. Print i − 1 spaces.
  9. Print 4 × n + 5 − 2 × i stars.
  10. Move to the next line.

Key takeaways

  • A heart is two pyramids on top of one inverted pyramid.
  • The gap between the lobes shrinks by two spaces per row, ending at one space.
  • The point starts at 4n + 3 stars so that it lines up with the lobes above it.
  • Changing n makes the whole heart bigger or smaller; the proportions stay the same.

Heart Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 3;

    // the two round lobes
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        for (int j = 1; j <= 2 * i + 1; j++)
            printf("*");
        for (int j = 1; j <= 2 * (n - i) + 1; j++)
            printf(" ");
        for (int j = 1; j <= 2 * i + 1; j++)
            printf("*");
        printf("\n");
    }

    // the point: an inverted pyramid
    for (int i = 1; i <= 2 * n + 2; i++) {
        for (int j = 1; j <= i - 1; j++)
            printf(" ");
        for (int j = 1; j <= 4 * n + 5 - 2 * i; j++)
            printf("*");
        printf("\n");
    }
    return 0;
}

Heart Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 3;

    // the two round lobes
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i + 1; j++)
            cout << "*";
        for (int j = 1; j <= 2 * (n - i) + 1; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i + 1; j++)
            cout << "*";
        cout << endl;
    }

    // the point: an inverted pyramid
    for (int i = 1; i <= 2 * n + 2; i++) {
        for (int j = 1; j <= i - 1; j++)
            cout << " ";
        for (int j = 1; j <= 4 * n + 5 - 2 * i; j++)
            cout << "*";
        cout << endl;
    }
    return 0;
}

Heart Star Pattern in Java

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

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

        // the point: an inverted pyramid
        for (int i = 1; i <= 2 * n + 2; i++) {
            for (int j = 1; j <= i - 1; j++)
                System.out.print(" ");
            for (int j = 1; j <= 4 * n + 5 - 2 * i; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Heart Star Pattern in Python

n = 3

# the two round lobes
for i in range(1, n + 1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, 2 * i + 2):
        print("*", end="")
    for j in range(1, 2 * (n - i) + 2):
        print(" ", end="")
    for j in range(1, 2 * i + 2):
        print("*", end="")
    print()

# the point: an inverted pyramid
for i in range(1, 2 * n + 2 + 1):
    for j in range(1, i):
        print(" ", end="")
    for j in range(1, 4 * n + 5 - 2 * i + 1):
        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 heart bigger?

Raise n. With n = 3 the lobes are 3 rows tall and the heart is 15 characters wide; with n = 5 it is 23 wide. Every width is worked out from n, so nothing else needs to change.

Can I draw the heart with a formula instead?

Yes. The curve (x² + y² − 1)³ − x²y³ ≤ 0 fills a heart shape, and you can test it for every cell of a grid. It gives a smoother outline but needs floating-point maths; the two-pyramid version only needs whole numbers and is easier to explain in an exam.

How do I print a hollow heart?

Keep the same loops but print a star only at the first and last position of each block, and on the top row of each lobe. The technique is the same one used in the hollow pyramid star pattern.

Why does the lower part loop to 2n + 2?

The point has to shrink from 4n + 3 stars to 1 star, two at a time. That takes (4n + 3 + 1) / 2 = 2n + 2 rows.

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.