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
Letter K Star Pattern output
Output (7 rows):
* * * * ** * ** * * * *

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.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
n − 3positions in the row. Print stars wherej = 1 or (i − (n + 1) / 2) × (i − (n + 1) / 2) = (j − 1) × (j − 1); print spaces everywhere else. - 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 − 1equals the distance from the middle row. - Comparing squares avoids
abs(), which needs an extra header in C. - The letter is
n − 3columns 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.









