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

Alphabet Diamond Pattern

The alphabet diamond prints letters in the shape of a diamond: A on top, then ABC, ABCDE and ABCDEFG across the middle, and back down to A. Every row starts at A.

Quick answer: Each row i prints n βˆ’ i spaces and then the first 2i βˆ’ 1 letters of the alphabet. Run i from 1 to n for the top half and from n βˆ’ 1 back down to 1 for the bottom half, so the widest row appears once.

This program is part of the String Patterns collection.

Table of contents8 sections
  1. Output
  2. How the alphabet diamond 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 Diamond Pattern output

Output (n = 4):

   A
  ABC
 ABCDE
ABCDEFG
 ABCDE
  ABC
   A
Alphabet Diamond Pattern layout diagram, row by row (n = 4)
How each row of the alphabet diamond pattern is built (n = 4). Blue cells are printed characters; grey dots are spaces.

How the alphabet diamond pattern works

This is the star diamond with each star replaced by a letter. Because the letter depends only on the position in the row, 'A' + j βˆ’ 1, every row spells the start of the alphabet. The widest row has 2n βˆ’ 1 letters, so n = 4 reaches G. The same loop prints both halves; only the order of the rows changes.

  1. Run the outer loop for i from 1 to n, one row per pass (upper half).
  2. Print n βˆ’ i spaces.
  3. Print 2 Γ— i βˆ’ 1 letters (letter number j).
  4. Move to the next line.
  5. Run the outer loop for i from n βˆ’ 1 down to 1, one row per pass (lower half).
  6. Print n βˆ’ i spaces.
  7. Print 2 Γ— i βˆ’ 1 letters (letter number j).
  8. Move to the next line.

Key takeaways

  • Every row prints the first 2i βˆ’ 1 letters, starting at A.
  • Leading spaces n βˆ’ i centre the rows, as in a star pyramid.
  • The lower half starts at n βˆ’ 1 so the widest row is printed once.
  • The widest row ends at letter 2n βˆ’ 1, so n must be 13 or less.

Alphabet Diamond Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;

    // upper half
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        for (int j = 1; j <= 2 * i - 1; j++)
            printf("%c", 'A' + j - 1);
        printf("\n");
    }

    // lower half
    for (int i = n - 1; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        for (int j = 1; j <= 2 * i - 1; j++)
            printf("%c", 'A' + j - 1);
        printf("\n");
    }
    return 0;
}

Alphabet Diamond Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;

    // upper half
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++)
            cout << char('A' + j - 1);
        cout << endl;
    }

    // lower half
    for (int i = n - 1; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++)
            cout << char('A' + j - 1);
        cout << endl;
    }
    return 0;
}

Alphabet Diamond Pattern in Java

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

        // upper half
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= 2 * i - 1; j++)
                System.out.print((char) ('A' + j - 1));
            System.out.println();
        }

        // lower half
        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= 2 * i - 1; j++)
                System.out.print((char) ('A' + j - 1));
            System.out.println();
        }
    }
}

Alphabet Diamond Pattern in Python

n = 4

# upper half
for i in range(1, n + 1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, 2 * i):
        print(chr(ord('A') + j - 1), end="")
    print()

# lower half
for i in range(n - 1, 0, -1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, 2 * i):
        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 make each row a palindrome (A, ABA, ABCBA)?

Print letters 1 to i and then i βˆ’ 1 back to 1, as in the alphabet palindrome pyramid, for both halves.

Why must n be 13 or less?

The widest row needs 2n βˆ’ 1 letters. With n = 14 that is 27, one more than the alphabet has, and the last character is no longer a letter.

How do I add spaces between the letters?

Print each letter with a space after it and double the leading spaces. The diamond becomes wider but stays centred.

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 7 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.