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

K Shape Star Pattern (Right Half Hourglass)

Read summary with AI tools

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
  1. K Shape Star Pattern output
  2. How the k shape star pattern works
  3. K Shape Star Pattern in C
  4. K Shape Star Pattern in C++
  5. K Shape Star Pattern in Java
  6. K Shape Star Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

K Shape Star Pattern output

Output (n = 5):

* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
K Shape Star Pattern layout diagram, row by row (n = 5)
How each row of the k shape star pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from n down to 1, one row per pass (upper half: shrinking rows).
  2. Print i stars.
  3. Move to the next line.
  4. Run the outer loop for i from 2 to n, one row per pass (lower half: growing rows).
  5. Print i stars.
  6. Move to the next line.

Key takeaways

  • The shape is an inverted triangle followed by a triangle, both left-aligned.
  • Both loops print i stars; 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.

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.