Diamond-Shaped Alphabet Pattern in C

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

Stack the inverted V from program 33 on top of a flipped copy: nine rows total, widest at E, narrowest at A. The trick is running the bottom outer loop from D down to A so the middle row appears only once.

⭐ Pattern Output

Nine rows; each line is 9 characters wide (spaces included), matching the two inner loops (5 + 4 positions):

Output
    A    
   B B   
  C   C 
 D     D 
E       E
 D     D 
  C   C 
   B B   
    A    
1

Complete C Program ('A''E')

Part one: i from 65 to 69. Part two: i from 68 down to 65. Inner loops are identical in both parts.

c
#include <stdio.h>

int main() {
    int i, j, k;

    // First part: A through E
    for (i = 65; i <= 69; ++i) {
        for (j = 69; j >= 65; --j) {
            if (i == j) {
                printf("%c", j);
            } else {
                printf(" ");
            }
        }
        for (k = 66; k <= 69; ++k) {
            if (i == k) {
                printf("%c", k);
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    // Second part: D through A (avoid duplicate E row)
    for (i = 68; i >= 65; --i) {
        for (j = 69; j >= 65; --j) {
            if (i == j) {
                printf("%c", j);
            } else {
                printf(" ");
            }
        }
        for (k = 66; k <= 69; ++k) {
            if (i == k) {
                printf("%c", k);
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Top outer loop (grow the V)

i runs 6569 (AE). Each larger i places the letter farther out on both diagonals, so the hollow shape opens toward the widest E row.

2

Two inner passes per row

First, j scans E down to A; only when i == j do you print that letter, else a space—this draws the left/up leg. Then k scans B through E; only when i == k do you print—this draws the right/up leg. Together they place two letters on most rows (one letter on the first and last rows).

j, k
3

Bottom outer loop (shrink the V)

The second part runs i from 68 down to 65 (DA) with the same inner loops pasted again. That rebuilds the arms in reverse so the diamond closes symmetrically.

4

Why the bottom starts at D

The top half already printed the single-wide E row. Continuing i from 68 skips repeating that middle line while still mirroring every narrower row.

E
=

Nine rows, two passes each

Five growing rows plus four shrinking rows; every row does two full scans over the letter ranges. That is O(n²) character decisions for n letters along each leg.

2

Variation — User Input (half height)

rows is the number of letters in the top half (e.g. 3AC). Bottom loop runs endChar - 1 down to startChar.

c
#include <stdio.h>

int main() {
    int rows;
    int i, j, k;
    char startChar, endChar;

    printf("Enter the number of rows (half): ");
    scanf("%d", &rows);

    startChar = 'A';
    endChar = (char)('A' + rows - 1);

    for (i = startChar; i <= endChar; ++i) {
        for (j = endChar; j >= startChar; --j) {
            if (i == j) {
                printf("%c", j);
            } else {
                printf(" ");
            }
        }
        for (k = startChar + 1; k <= endChar; ++k) {
            if (i == k) {
                printf("%c", k);
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    for (i = endChar - 1; i >= startChar; --i) {
        for (j = endChar; j >= startChar; --j) {
            if (i == j) {
                printf("%c", j);
            } else {
                printf(" ");
            }
        }
        for (k = startChar + 1; k <= endChar; ++k) {
            if (i == k) {
                printf("%c", k);
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Factor the inner two loops into a small function to remove duplication
  • Fill the interior for a solid diamond of letters
  • Use char literals for all bounds instead of ASCII numbers

Avoid

  • Starting the bottom half at endChar unless you intentionally want two center rows
  • Forgetting that rows in the variation means half of the diamond, not total line count

Key Takeaways

1

Two outer passes share the same j / k conditional printing.

2

Bottom pass begins at endChar - 1 to avoid duplicating the widest row.

3

Total rows = 2 × (endChar - startChar) + 1 for this layout.

4

O(n²) for half-height n letters.

❓ Frequently Asked Questions

Part one prints rows for i = A … E using the inverted-V logic. Part two prints i = D … A with the same inner loops, so the pattern narrows again symmetrically.
The row for 'E' already ended part one. Beginning at 69 would draw that widest line twice.
Eighty-one is the character count for this fixed AE version (nine rows, nine columns). For variable half-height n, work grows on the order of n²; do not confuse literal counts with the definition of big-O.
O(n²) when n is the number of letters from A to the peak (each of about 2n - 1 rows does Theta(n) inner work).

Continue With Number Patterns

The same two-part idea—grow then shrink—works for numeric diamonds too.

C Number Patterns →
Did you know?

You could generate the bottom half by reusing the top loop with a separate data array, but duplicating the loop body keeps the symmetry obvious for learners.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful