Diamond Alphabet & Stars in C

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

What You’ll Learn

Build a vertical diamond where each horizontal line is the same letter separated by *, widening to the middle then narrowing again.

Compare C star diamonds (geometry only) and program 15 (mirror with a star block).

⭐ Pattern Output

Half height 5 (ASCII assumed for 'A' + offset):

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

Complete C Program (Half Height 5)

i + 64 matches 'A' + i - 1 on ASCII. Prefer ch = (char)('A' + i - 1) for clarity.

c
#include <stdio.h>

int main() {
    int i, j;
    char ch;

    for (i = 1; i <= 5; ++i) {
        ch = (char)('A' + i - 1);
        for (j = 1; j < i * 2; ++j) {
            if (j % 2 == 0) {
                printf("*");
            } else {
                printf("%c", ch);
            }
        }
        printf("\n");
    }

    for (i = 4; i >= 1; --i) {
        ch = (char)('A' + i - 1);
        for (j = 1; j < i * 2; ++j) {
            if (j % 2 == 0) {
                printf("*");
            } else {
                printf("%c", ch);
            }
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Upper half: i = 1 .. n

Each row gets its own row letter: ch = (char)('A' + i - 1). Row 1 is all A, row 2 alternates B and *, and so on until the middle row uses nth letter.

Up
2

Inner stripe: j from 1 to 2i - 1

for (j = 1; j < i * 2; ++j) runs an odd number of positions. Even j prints *; odd j prints ch. That yields B*B, C*C*C, … because stars sit between repeated row letters.

Stripe
3

Lower half: i = n-1 .. 1

After the peak row at i = n, a second outer loop counts i down to 1 with the same inner logic. That mirrors the upper half without printing the widest line twice.

Down
4

Why width is 2i - 1

For row index i you need i letters and i - 1 gaps between them, which is 2i - 1 symbols. At n = 5 the middle line has nine tokens: E*E*E*E*E.

Peak
=

Diamond cost

Upper and lower halves each sum 1 + 3 + … + (2n-1) = n² cells, minus one shared middle row when you split loops as in the sample. Overall print volume is Θ(n²) for half-height n.

2

Variation — User Input (Half Height)

Lower loop i = rows - 1 down to 1.

c
#include <stdio.h>

int main() {
    int rows, i, j;
    char ch;

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

    for (i = 1; i <= rows; ++i) {
        ch = (char)('A' + i - 1);
        for (j = 1; j < i * 2; ++j) {
            if (j % 2 == 0) {
                printf("*");
            } else {
                printf("%c", ch);
            }
        }
        printf("\n");
    }

    for (i = rows - 1; i >= 1; --i) {
        ch = (char)('A' + i - 1);
        for (j = 1; j < i * 2; ++j) {
            if (j % 2 == 0) {
                printf("*");
            } else {
                printf("%c", ch);
            }
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add leading spaces so the diamond is centered in the terminal
  • Swap * for - or another separator
  • Cap rows so 'A' + rows - 1 <= 'Z'

Avoid

  • Second loop from rows instead of rows - 1 (duplicates the widest row)
  • Using if (j % 2) with the same branches as this program without swapping — parity flips

Key Takeaways

1

Odd j → letter, even j* (with this loop layout).

2

j < 2*i gives 2i - 1 symbols per row.

3

Mirror with i = n-1 .. 1 after 1 .. n.

4

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

❓ Frequently Asked Questions

In this code, even j prints *; odd j prints the row letter. If you flipped the branches, you would flip the pattern.
Only the upper loop runs once (printing A); the lower loop starts at 0 and does nothing.
It assumes ASCII (or compatible) encoding. 'A' + i - 1 states the intent more clearly.
O(n²) for half-height n.

Explore More C Alphabet Patterns!

Two-phase loops (up then down) are the usual way to code diamonds without repeating the center line.

All Alphabet Patterns →
Did you know?

On row i the letter appears i times and * appears i - 1 times, for a total of 2i - 1 characters.

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