V-Shaped Alphabet Pattern in C

Beginner
⏱️ 9 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

Print letters only on the two diagonals that form a V, with spaces everywhere else. The last row prints a single vertex letter (E) because the right diagonal intentionally skips the last letter. Compare Program 20 (diagonal drills) and Program 21 (diamond symmetry). Includes a live preview, worked C examples, edge cases, and complexity.

Two Legs

Left + right

Two diagonal scans meet at the bottom vertex.

i == j

Main diagonal

Left leg prints only when row equals column.

Skip Vertex

Right starts at n-1

Right scan avoids duplicating the tip letter.

Width 2n+1

(n+1) + n

For A..E (n=4), each line spans 9 columns.

Live Preview

End letter

Pick an end letter (A–F) and draw the V.

O(n²)

Complexity

n rows × O(n) column scans each.

Introduction

A V-shaped alphabet pattern places one letter on the main diagonal and one on a mirrored diagonal each row, filling the rest with spaces so the shape reads as a V in the console.

In C you walk row index i, scan left columns with i == j, then scan right columns from n-1 down so the tip letter prints once.

Why it matters?

It locks in conditional diagonal printing and careful vertex handling — skills that transfer to X shapes, borders, and other sparse letter grids.

Key Highlights

Left leg

Print when i == j.

Right leg

Scan k from n-1 to 0.

Spaces

Fill non-diagonal cells.

One tip

Bottom vertex prints once.

In short: for each row i, scan left 0..n printing when i == j, then scan right n-1..0 printing when i == k, then call printf("\n").

📝 Problem & Approach

Given an end letter (or fixed E), print a V of letters on two diagonals with spaces elsewhere and a single bottom vertex.

c
// Five rows (end = E, width 9)
// A       A
//  B     B
//   C   C
//    D D
//     E

Inputs & Outputs

ItemTypeDescription
end / nchar / intEnd letter; n = end - 'A' (4 for E). Rows = n+1.
Printed outputtextV of width 2n+1 with letters on diagonals and spaces elsewhere.

Minimal workflow

Pseudocode
n = end - 'A'
for i from 0 to n:
    for j from 0 to n:                 // left leg
        print (i == j ? letter[j] : " ")
    for k from n-1 down to 0:          // right leg (skip tip)
        print (i == k ? letter[k] : " ")
    print newline

Approach comparison

ApproachIdeaBest for
Two diagonal scansLeft 0..n + right (n-1)..0 with i==colMatching this classic sample
Single width loopMap columns 0..(2n) to left/right conditionsOne inner loop; same visuals

⚡ Quick Reference

GoalPattern
Alphabet + nchar alpha[] = "ABCDEFG..."; int n = 4;
Rowsfor (int i = 0; i <= n; i++)
Left legfor (int j = 0; j <= n; j++) printf("%c", i == j ? alpha[j] : ' ');
Right legfor (int k = n - 1; k >= 0; k--) printf("%c", i == k ? alpha[k] : ' ');
Symmetric pyramid nextSee Program 32

📋 Left vs Right vs Vertex

Four roles that build the V without a double tip.

j = 0..n
left

Main diagonal via i == j

k = n-1..0
right

Mirrored leg; skips tip index

spaces
fill

Keep columns aligned

printf("\n")
break

Ends each full-width row

Context

When This Pattern Shows Up

Reach for this when teaching sparse diagonal printing and a single shared vertex.

  1. After filled rows

    Step up from full letter rows to sparse diagonals.

  2. Diagonal condition drills

    Practice i == j with spaces for alignment.

  3. Vertex handling

    Skip the tip on one leg so it prints once.

  4. Bridge to Program 32

    Next builds centered palindrome pyramids with spaces.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: two diagonal scans with a right leg that starts at n-1 is the cleanest way to draw a V with a single tip letter.

🔮 Live Preview

Choose an end letter from A to F and draw the V-shaped alphabet pattern in the browser.

Try E (classic sample) or C (smaller V). Preview allows A–F. Use a monospace view for alignment.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — fixed A–E, scanf end letter, and a print_cell helper. Click View Output to reveal sample console results.

📚 Getting Started

Print a five-row V with a single E at the tip.

Example 1 — Fixed A–E

Two scans per row: left-to-right (A..E) and right-to-left (D..A). Letters print only when the row matches the column.

c
#include <stdio.h>

int main() {
    char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i, j, k;

    for (i = 0; i <= 4; i++) {
        for (j = 0; j <= 4; j++) {
            if (i == j)
                printf("%c", alpha[j]);
            else
                printf(" ");
        }

        for (k = 3; k >= 0; k--) {
            if (i == k)
                printf("%c", alpha[k]);
            else
                printf(" ");
        }

        printf("\n");
    }

    return 0;
}

How It Works

When i = 2, the left scan prints C at column 2 and the right scan prints C when k = 2. On the last row, only the left scan can print E.

📈 Practical Variant

Let the user pick the end letter (like E).

Example 2 — End Letter Input

The right scan starts at end - 1 so the vertex prints once. Check scanf and validate a single A–Z character in real apps.

c
#include <stdio.h>

int main() {
    char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char end;
    int n, i, j, k;

    printf("Enter top letter (like E): ");
    scanf(" %c", &end);

    n = end - 'A';

    for (i = 0; i <= n; i++) {
        for (j = 0; j <= n; j++)
            printf("%c", i == j ? alpha[j] : ' ');

        for (k = n - 1; k >= 0; k--)
            printf("%c", i == k ? alpha[k] : ' ');

        printf("\n");
    }

    return 0;
}

How It Works

n = end - 'A' scales both scans. For end = C, width is 2×2+1 = 5 and the tip is a single C.

⚡ Helper Style

Same V with a shared cell helper for both legs.

Example 3 — Helper Function

Often clearer: one function applies the diagonal rule so left and right loops stay thin.

c
#include <stdio.h>

void print_cell(char alpha[], int row, int col) {
    printf("%c", row == col ? alpha[col] : ' ');
}

int main() {
    char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int n = 4;
    int i, j, k;

    for (i = 0; i <= n; i++) {
        for (j = 0; j <= n; j++)
            print_cell(alpha, i, j);

        for (k = n - 1; k >= 0; k--)
            print_cell(alpha, i, k);

        printf("\n");
    }

    return 0;
}

How It Works

print_cell owns the row == col rule once. The right loop still starts at n - 1 to keep a single tip.

🧠 How the Algorithm Prints Rows

1

Outer loop chooses the row letter

Row index i runs from 0..n which maps to A..end (A..E when n = 4).

Rows
2

Left leg (main diagonal)

Loop j = 0..n. Print the letter only when i == j; otherwise print a space.

Left
3

Right leg (skip tip to keep a single vertex)

Loop k = n-1..0 (D..A for E). Starting one below the tip keeps the last row as a single letter.

Right
4

Width stays 2n+1

Left block has n+1 columns; right block has n columns. Total width is 2n+1 (9 for A..E).

Width
=

Two diagonals form a V

Each row prints one letter on the left diagonal plus one on the right (except the last row) — O(n²) time.

🔎 Worked Walkthrough — End = E (n = 4)

Trace each row’s diagonal hits and the resulting 9-column line.

iLeft hitRight hitPrinted row
0A at j=0A at k=0A A
1B at j=1B at k=1B B
2C at j=2C at k=2C C
3D at j=3D at k=3D D
4E at j=4(none)E

Width is always 2×4+1 = 9. The tip row has no right-leg match because k never equals 4.

Use Cases

Where this V-shaped alphabet pattern shows up beyond the homework prompt.

1. Diagonal Labs

Clearest demo of letter-only diagonals with space fill.

Example: temporarily print . instead of spaces to see columns.

2. Vertex Practice

Learn why one leg must skip the tip index.

Example: start right at n and watch a double E.

3. Index Mapping

Practice n = end - 'A' with an alphabet array.

Example: scale from E to H without rewriting loops.

4. Helper Extraction

Factor the diagonal rule into print_cell (Example 3).

Example: reuse print_cell for an X-shaped variant later.

5. Complexity Intuition

Full grid scans make O(n²) easy to see even when few letters print.

Example: 5 rows × 9 cells = 45 writes.

6. Bridge to Program 32

Next centers palindrome alphabet pyramids with leading spaces.

Example: continue to Program 32.

Pro Tip: say “left i==j, right i==k from n-1, spaces elsewhere” before coding — that story prevents a double tip.

Advantages

Why this pattern earns a spot after filled alphabet rows.

  1. 1. Instant Visual Feedback

    A broken diagonal or double tip shows up immediately.

  2. 2. One Shared Rule

    Both legs reuse row == col with spaces for fill.

  3. 3. Scales Cleanly

    Change n and the whole V grows.

  4. 4. Helper-Friendly

    print_cell keeps both legs short and readable.

Pro Tip: check alignment in a monospace font; proportional fonts make spaces look uneven.

Usage Tips

Small habits that keep V-shaped alphabet patterns clean.

  1. 1. Start the Right Leg at n-1

    Starting at n duplicates the tip letter.

  2. 2. Always Print Spaces Off-Diagonal

    Skipping spaces collapses the V into packed letters.

  3. 3. Set n from the End Letter

    Use n = end - 'A' so scaling stays automatic.

  4. 4. Check scanf

    Require a single A–Z character; use toupper if needed.

  5. 5. Check in Monospace

    Proportional fonts hide whether columns really align.

Pro Tip: if the last row shows E E, the right scan almost certainly started at k = n.

Common Pitfalls

Mistakes that commonly break V-shaped alphabet patterns.

  1. 1. Starting the Right Leg at n

    Duplicates the tip letter on the last row.

    → Start the right scan at k = n - 1.

  2. 2. Skipping Spaces

    Writing only letters collapses the V.

    → Print a space whenever row != col.

  3. 3. End Letter Past Z

    Char math can walk past the alphabet.

    → Validate a single A–Z letter.

  4. 4. Unchecked scanf

    Empty or multi-character input leaves end unused or only takes the first char.

    → Check scanf’s return value and require a single A–Z letter.

  5. 5. Wrong Comparison Operands

    Comparing to the wrong index prints letters on the wrong diagonal.

    → Keep i == j / i == k against the current column.

Edge Cases

Check these inputs before calling the solution done.

end = A

Single letter

Output is just A (right leg empty).

end = E

Classic sample

5 rows × width 9 with tip E.

end = C

Smaller V

3 rows × width 5 (Example 2).

Lowercase

Case mismatch

Convert lowercase with toupper from <ctype.h> if needed.

Bad input

Empty / multi-char

Check scanf’s return value before using end.

Debug dots

See columns

Temporarily print . instead of spaces.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Duplicate the tip on purpose

  • Start the right leg at n once
  • Confirm why the sample uses n-1

2. Extract print_cell

  • Use a helper (Example 3)
  • Keep both legs calling it

3. Scale to H

  • Set end = H and recompute n
  • Check width = 2n+1

4. Continue to Program 32

Notes

  • Two legs. Left 0..n and right (n-1)..0 form the V.
  • Print letters only when row == col; otherwise print a space.
  • Row width is always 2n + 1 (9 for A..E).
  • Program 32 builds centered palindrome pyramids (A, ABA, ABCBA, …).

Quick Takeaway: scan left with i == j, scan right from n-1 with i == k, fill spaces, skip a double tip, then break the line.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Inline / input (Examples 1–2)O(n²)O(1) (plus alphabet source)
Helper function (Example 3)O(n²)O(1)

For n+1 letters there are n+1 rows and each row scans O(n) columns across both blocks, so total work is O(n²).

Wrap Up

🎉 Conclusion

The V-shaped alphabet pattern is a sparse nested-loop exercise with lasting payoff: diagonal conditions, space fill for alignment, and a right leg that skips the tip so the vertex prints once. Master the classic A…E sample, then try user input and the helper rewrite.

Practice the three examples above, then continue to Program 32’s symmetric alphabet pyramid.

Left 0..n with i == j, right (n-1)..0 with i == k, spaces elsewhere, then break the line.

💡 Best Practices

✅ Do

  • Start the right leg at k = n - 1
  • Print spaces on non-diagonal cells
  • Derive n from the end letter
  • Validate a single A–Z end letter for input variants
  • State O(n²) when asked about complexity

❌ Don’t

  • Start the right leg at n (duplicates the tip)
  • Skip spaces and pack letters together
  • Hard-code n without updating the alphabet source
  • Skip validating end-letter input
  • Call printf("\n") inside either column loop

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the V-shaped alphabet pattern the beginner-friendly way.

5
Core concepts
= 02

Condition

i == col

Code
1 03

Right

Start at n-1

Code
W 04

Width

2n+1 columns

Shape
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

If the right scan included E, the last row would print E twice (one on each side), breaking the single vertex at the bottom tip of the V.
For n letters, the left block is n columns and the right block is n−1 columns, so total width is 2n−1.
Usually two (one per leg), except the bottom row prints one letter because the right loop cannot match the last letter.
O(n²) because there are n rows and each row scans O(n) positions across both blocks.
Spaces keep column alignment so the two diagonals form a visible V in a monospace console.
Check scanf(" %c", &end) == 1, require a single A–Z character, and reject invalid input.
Yes. Set n = end − 'A', scan j from 0 to n on the left, and scan k from n−1 down to 0 on the right.
Program 20 also practices diagonal alignment with letters; this page focuses on a V made from two opposing diagonal scans that meet at one vertex.

Did you Know? 🔊

Outer i is the row index (A..E). Left scan prints only when i == j (main diagonal). Right scan runs from D down to A so the bottom vertex letter (E) appears once.

Continue to Alphabet Pattern 32

Next up: centered symmetric alphabet pyramids with palindromic rows like A, ABA, ABCBA, and ABCDEDCBA.

Program 32 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