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

Letter K Star Pattern

Read summary with AI tools

The letter K star pattern draws a straight stem and two arms that meet the stem in the middle, one going up and one going down.

Quick answer: Use 7 rows and 4 columns. Print the stem in column 1, and print an arm star where j − 1 equals the distance from the middle row, |i − 4|. The test (i − 4)² == (j − 1)² does that without an absolute-value function.

This program is part of the Star Patterns collection.

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

Letter K Star Pattern output

Output (7 rows):

*  *
* *
**
*
**
* *
*  *
Letter K Star Pattern layout diagram, row by row (7 rows)
How each row of the letter k star pattern is built (7 rows). Blue cells are printed characters; grey dots are spaces.

How the letter k star pattern works

Both arms are diagonal lines that start at the stem on the middle row and move one column right for every row away from it. So the column of an arm star is one more than the distance from the middle row. Squaring both sides turns “distance equals offset” into a plain arithmetic test that works in every language. Every letter on this site is drawn on a grid of rows and columns, and each stroke is one simple test on i and j.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n − 3 positions in the row. Print stars where j = 1 or (i − (n + 1) / 2) × (i − (n + 1) / 2) = (j − 1) × (j − 1); print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • The arms are two diagonals that meet the stem on the middle row.
  • An arm star sits where j − 1 equals the distance from the middle row.
  • Comparing squares avoids abs(), which needs an extra header in C.
  • The letter is n − 3 columns wide: 4 columns for 7 rows.

Letter K Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 7;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - 3; j++) {
            if (j == 1 || (i - (n + 1) / 2) * (i - (n + 1) / 2) == (j - 1) * (j - 1))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Letter K Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 7;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - 3; j++) {
            if (j == 1 || (i - (n + 1) / 2) * (i - (n + 1) / 2) == (j - 1) * (j - 1))
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Letter K Star Pattern in Java

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

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - 3; j++) {
                if (j == 1 || (i - (n + 1) / 2) * (i - (n + 1) / 2) == (j - 1) * (j - 1))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Letter K Star Pattern in Python

n = 7

for i in range(1, n + 1):
    for j in range(1, n - 3 + 1):
        if j == 1 or (i - (n + 1) // 2) * (i - (n + 1) // 2) == (j - 1) * (j - 1):
            print("*", end="")
        else:
            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 compare squares instead of using abs()?

(i − 4)² == (j − 1)² is true exactly when |i − 4| == j − 1, because j − 1 is never negative here. It saves including <stdlib.h> in C.

How is it different from the K shape star pattern?

The K shape is a filled shape made of two triangles. This letter K is drawn with thin strokes, the way you would write it.

How do I make the arms longer?

Raise n to another odd number, such as 9, and set the width to (n + 1) / 2 columns so the arms reach the corners.

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.