The K shape star pattern prints a shrinking triangle and then a growing one, both against the left edge, so the outline looks like the letter K lying on its back. It is also called the right half hourglass.
Quick answer: Run the rows from n down to 1, printing i stars each followed by a space, then run them from 2 back up to n. Starting the second loop at 2 keeps the single star in the middle from appearing twice.
This program is part of the Star Patterns collection.
Table of contents8 sections
K Shape Star Pattern output
Output (n = 5):
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *

How the k shape star pattern works
Both halves are right triangles printed with the same inner loop; only the order of the rows changes. The first loop counts down, so the rows get shorter, and the second counts up, so they get longer again. Because each star is followed by a space, every row is twice as wide as its star count, which gives the spread-out look of the usual K pattern.
- Run the outer loop for
ifromndown to1, one row per pass (upper half: shrinking rows). - Print
istars. - Move to the next line.
- Run the outer loop for
ifrom2ton, one row per pass (lower half: growing rows). - Print
istars. - Move to the next line.
Key takeaways
- The shape is an inverted triangle followed by a triangle, both left-aligned.
- Both loops print
istars; only the row order differs. - Starting the second loop at 2 avoids a repeated middle row.
- Printing
"* "instead of"*"spreads the stars out.
K Shape Star Pattern in C
#include <stdio.h>
int main(void) {
int n = 5;
// upper half: shrinking rows
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
// lower half: growing rows
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}K Shape Star Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
// upper half: shrinking rows
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++)
cout << "* ";
cout << endl;
}
// lower half: growing rows
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++)
cout << "* ";
cout << endl;
}
return 0;
}K Shape Star Pattern in Java
public class KShapeStar {
public static void main(String[] args) {
int n = 5;
// upper half: shrinking rows
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++)
System.out.print("* ");
System.out.println();
}
// lower half: growing rows
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++)
System.out.print("* ");
System.out.println();
}
}
}K Shape Star Pattern in Python
n = 5
# upper half: shrinking rows
for i in range(n, 0, -1):
for j in range(1, i + 1):
print("* ", end="")
print()
# lower half: growing rows
for i in range(2, n + 1):
for j in range(1, 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
Why is it called a K pattern?
Its left edge is a straight line and the right side forms two arms that meet in the middle, which is the shape of a K. It is also the left half of the hourglass star pattern.
How do I make the mirrored version?
Add n − i double spaces before each row. The rows then line up on the right, as in the mirrored K shape.
How is this different from the half diamond?
The half diamond grows first and then shrinks, so it points to the right. The K shape shrinks first and then grows, so the narrow part is in the middle.











