C Alphabet Diamond Pattern (With Stars)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A diamond alphabet with stars widens then shrinks: each row repeats one letter with * between copies (B*B, C*C*C), mirrored so the widest middle row appears only once.

Remember
Rule: odd j → row letter; even j → '*'; upper 1..n then lower n-1..1

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     ← half height n = 5

Unlike Program 15 (stars filling a center gap between alphabet wings), here one letter owns the whole row and stars sit between letter copies.

How to Solve It

Print an upper half that grows, then a lower half that shrinks from n-1, alternating letter and star by column index.

MethodIdeaBest for
Two outer loopsUpper 1..n, lower n-1..1, shared inner alternationLearning, interviews, fixed demos
print_row helperOne function owns the letter/* run; halves only choose iLess duplication, cleaner structure

Pseudocode

Pseudocode
for i from 1 to n:
    ch = 'A' + i - 1
    for j from 1 to 2*i - 1:
        print '*' if j is even else ch
    print newline
for i from n - 1 down to 1:
    (same inner loop)

Cheat sheet

GoalPattern
Upper halffor (i = 1; i <= n; i++)
Lower halffor (i = n - 1; i >= 1; i--)
Odd widthfor (j = 1; j < i * 2; j++) → 2i-1 chars
Alternationif (j % 2 == 0) printf("*"); else printf("%c", ch);
Row letterch = (char)('A' + i - 1);
End the rowprintf("\n");
A–Z-safe heightClamp n to 1–26

Printing Letters vs Starting a New Line

APIEffectUse for
printf("%c", ch) / printf("*")Stays on the same lineEach letter and each star
printf("\n")Ends the current lineAfter the alternating run

Print each symbol without a newline, then end the row once.

Live Preview

Change the half height and the diamond updates instantly — including middle letter and total rows.

Whole numbers from 1 to 8. Total rows = 2n - 1; middle letter is A + n - 1.

Live result n = 5 · middle E · 9 rows
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

Worked Walkthrough — n = 3

Trace each row height, the letter, the odd-width run, and where the mirror starts.

HalfiLetterWidthPrinted row
Upper1A1A
Upper2B3B*B
Upper3C5C*C*C
Lower2B3B*B
Lower1A1A

Lower half starts at n - 1 so C*C*C is not printed twice. Width formula: 2i - 1.

C Programs

Three complete programs: fixed n = 5, half-height input, and a print_row helper. Use View Output to reveal sample results.

Example 1 — Fixed Half Height 5

Odd j prints the row letter; even j prints *. Upper half prints 1..5, lower half prints 4..1.

C
#include <stdio.h>

int main(void)
{
    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 grows. Row i uses letter 'A' + i - 1 and prints 2i - 1 characters.

2. Alternation. Even j prints *; odd j prints the row letter — so i = 3 becomes C*C*C.

3. Lower half mirrors. Starts at 4 so the widest row E*E*E*E*E appears only once.

Example 2 — Half Height Input

Uses a computed row letter ch = 'A' + i - 1. Check scanf in real apps.

C
#include <stdio.h>

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

    printf("Enter half height (like 5): ");
    scanf("%d", &n);

    for (i = 1; i <= n; 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 = n - 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;
}

How It Works

1. Same diamond rules. Only n changes the size — alternation and mirror stay identical.

2. Char from index. 'A' + i - 1 picks the letter for each row height.

3. Safer input tip. Prefer checking scanf and staying within A–Z:

Safer input
if (scanf("%d", &n) != 1 || n < 1 || n > 26)
{
    printf("Enter a half height from 1 to 26.\n");
    return 1;
}

Example 3 — print_row Helper

Same diamond, less duplicated code.

C
#include <stdio.h>

void print_row(int i)
{
    int j;
    char ch = (char)('A' + i - 1);

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

int main(void)
{
    int n = 5;
    int i;

    for (i = 1; i <= n; i++)
    {
        print_row(i);
    }

    for (i = n - 1; i >= 1; i--)
    {
        print_row(i);
    }

    return 0;
}

How It Works

1. One row printer. print_row(i) owns the letter/* alternation and the newline.

2. Halves only choose height. Upper loops 1..n; lower loops n-1..1 — no duplicated inner logic.

3. Easier to change. Want a different separator? Edit one function instead of two loops.

Edge Cases & Pitfalls

Check these before calling the solution done.

Lower from n

Doubled middle

Starting the lower half at n reprints the widest row. Always start at n - 1.

j <= i * 2

Even width

Using j <= i * 2 prints an extra character. Keep j < i * 2 for odd widths 1, 3, 5, …

Swap odd/even

Star first

Printing * on odd j starts each row with a star. Letters belong on odd positions.

n = 1

Single A

Output is just A; the lower half does not run — a good sanity check.

Past Z

Clamp to 26

Half height above 26 walks the middle letter past Z. Cap or reject the input.

Bad scanf

Uninitialized n

Check scanf("%d", &n) == 1 before looping so a failed read does not leave n garbage.

Time and Space Complexity

ProgramTimeExtra space
Two-loop / helper formsO(n²)O(1)

Upper half prints 1 + 3 + … + (2n - 1) = n² characters; the lower half adds almost the same without the middle row — still quadratic in n.

Key Takeaways

  • Two halves: grow 1..n, then shrink n-1..1 so the middle is unique.
  • Odd widths: each row prints 2i - 1 characters via j < i * 2.
  • Alternation: odd j → letter; even j → *.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row height up then down, print letter and * alternating across an odd-width run.

Frequently Asked Questions

j runs 1,2,3,... Odd positions print the row letter and even positions print '*', producing lines like B*B and C*C*C.
The inner loop checks the position: even columns print '*', odd columns print the row letter. That alternates symbols cleanly.
That prints 2*i-1 characters per row (1,3,5,...), which makes the pattern widen toward the center.
The upper half already printed the widest row at i=n. Starting from n-1 mirrors without duplicating the middle row.
printf("%c", ch) or printf("*") stays on the same line for each symbol. printf("\n") ends the row after the inner loop finishes.
Program 15 puts stars in the center of a symmetric alphabet row. This pattern repeats one letter per row and places stars between those letters, then mirrors vertically.
O(n²) for half height n because the total printed characters is proportional to 1+3+...+(2n-1) up and down.
Check scanf("%d", &n) == 1, require n ≥ 1, and cap at 26 so row letters stay within A–Z.

Did you know?

Upper half prints rows 1..n; lower half prints n-1..1 so the widest row appears once. Each row runs j = 1..(2i-1). Odd j prints the row letter, even j prints *.

Next: Sequential Pyramid

Right-aligned rows with a continuous running letter counter.

Program 22 tutorial →

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