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

Alphabet K Pattern

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
  1. Output
  2. How the alphabet k 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

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
Alphabet K Pattern layout diagram, row by row (n = 4)
How each row of the alphabet k pattern is built (n = 4). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from n down to 1, one row per pass (upper arm).
  2. Print i letters (letter number j).
  3. Move to the next line.
  4. Run the outer loop for i from 2 to n, one row per pass (lower arm).
  5. Print i letters (letter number j).
  6. 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 21 of 22 in String Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

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.

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.