The alphabet K pattern prints a shrinking alphabet triangle followed by a growing one, so the right side forms the two arms of a K. Every row starts at A, so the left edge is a straight column of As.
Quick answer: Run the rows from n down to 1, printing the first i letters each time, then run them from 2 back up to n. The letter is 'A' + j − 1, and starting the second loop at 2 avoids printing the single A twice.
This program is part of the String Patterns collection.
Table of contents8 sections
Alphabet K Pattern output
Output (n = 4):
A B C D A B C A B A A B A B C A B C D

How the alphabet k pattern works
The two arms are the alphabet triangle printed in two directions. Nothing about a row changes except the order: the first loop counts down, the second counts up. Because the letter comes from the position, the left edge is always A and each column always holds the same letter.
- Run the outer loop for
ifromndown to1, one row per pass (upper arm). - Print
iletters (letter numberj). - Move to the next line.
- Run the outer loop for
ifrom2ton, one row per pass (lower arm). - Print
iletters (letter numberj). - Move to the next line.
Key takeaways
- The K is a shrinking triangle then a growing one.
- Every row starts at A, so the left edge is straight.
- The second loop starts at 2 to avoid repeating the middle row.
- It is the letter version of the K shape star pattern.
Alphabet K Pattern in C
#include <stdio.h>
int main(void) {
int n = 4;
// upper arm
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++)
printf("%c ", 'A' + j - 1);
printf("\n");
}
// lower arm
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++)
printf("%c ", 'A' + j - 1);
printf("\n");
}
return 0;
}Alphabet K Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 4;
// upper arm
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++)
cout << char('A' + j - 1) << " ";
cout << endl;
}
// lower arm
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++)
cout << char('A' + j - 1) << " ";
cout << endl;
}
return 0;
}Alphabet K Pattern in Java
public class AlphabetK {
public static void main(String[] args) {
int n = 4;
// upper arm
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++)
System.out.print((char) ('A' + j - 1) + " ");
System.out.println();
}
// lower arm
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++)
System.out.print((char) ('A' + j - 1) + " ");
System.out.println();
}
}
}Alphabet K Pattern in Python
n = 4
# upper arm
for i in range(n, 0, -1):
for j in range(1, i + 1):
print(chr(ord('A') + j - 1), end=" ")
print()
# lower arm
for i in range(2, n + 1):
for j in range(1, i + 1):
print(chr(ord('A') + j - 1), 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 mirror it?
Print n − i double spaces before each row. The rows then line up on the right and the arms point to the left.
Can every row use its own letter instead (D D D D, C C C)?
Yes. Print 'A' + i − 1 instead of 'A' + j − 1.
How do I let the user choose the size?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.






