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
Heart Star Pattern output
Output (n = 3):
*** ***
***** *****
******* *******
***************
*************
***********
*********
*******
*****
***
*
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.
- Run the outer loop for
ifrom1ton, one row per pass (the two round lobes). - Print
n − ispaces. - Print
2 × i + 1stars. - Print
2 × (n − i) + 1spaces. - Print
2 × i + 1stars. - Move to the next line.
- Run the outer loop for
ifrom1to2 × n + 2, one row per pass (the point: an inverted pyramid). - Print
i − 1spaces. - Print
4 × n + 5 − 2 × istars. - 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 + 3stars so that it lines up with the lobes above it. - Changing
nmakes 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.











