Inverted Repeating-Letter Triangle in C

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

What You’ll Learn

Same repeating idea as Program 9 and Program 10, but widths shrink while letters still run E → A: EEEEE, DDDD, CCC, BB, A. This tutorial covers the inverted shape rule, ch-- after each row, a live preview, worked C examples, edge cases, and complexity.

Shape Rule

Repeat letter, grow width

First row prints EEEEE, then DDDD, down to a single A.

Outer Loop

Width shrinks

for (i = rows; i >= 1; i--) walks from widest row to narrowest.

Inner Loop

Repeat count

for (j = 1; j <= i; j++) prints ch exactly i times, then ch-- after the row.

printf vs putchar

Same line / next line

Letters use printf("%c", ch); end each row with printf("\n").

Live Preview

1–26 rows

Pick a row count and draw the inverted repeating triangle in the browser.

O(n²)

Complexity

Total letters = n(n+1)/2; extra memory stays O(1).

Introduction

An inverted repeating alphabet triangle starts wide and shrinks by one repeated letter on each new line, counting letters downward from the top of the alphabet range. With the right angle on the left, the console shows an upside-down staircase of identical letters per row.

In C you usually solve it with two nested for loops: the outer loop counts width down from rows to 1, the inner loop prints ch that many times, then printf("\n") and ch-- prepare the next shorter row.

Why it matters?

It locks in inverted outer bounds plus a per-row letter step. Once that clicks, growing repeats (Program 9), reverse growing (Program 10), and more letter variants become much easier.

Key Highlights

Widths Shrink

Row widths are 5, 4, 3, …, 1 (for five rows).

Letters Count Down

Rows use E, then D, then C … down to A.

ch-- After the Row

Print ch in the inner loop; decrement only after the newline.

Mirror of Programs 9 & 10

Same letters as Program 10 — inverted widths like Program 5.

In short: for i from rows down to 1, print ch exactly i times, call printf("\n"), then ch--.

📝 Problem & Approach

Given a positive integer rows (or a fixed top letter like 'E'), print a left-aligned inverted triangle where each row repeats one letter, widths shrink, and letters count downward.

c
// First 5 rows (conceptual shape)
// EEEEE
// DDDD
// CCC
// BB
// A

Inputs & Outputs

ItemTypeDescription
rowsintNumber of triangle lines (typically 1–26). Top letter = 'A' + rows - 1.
Printed outputtextLeft-aligned rows; first row repeats the top letter rows times, then widths shrink while letters step down.

Minimal workflow

Pseudocode
ch = 'A' + rows - 1
for i from rows down to 1:
    for j from 1 to i:
        print ch (no newline)
    print newline
    ch = ch - 1

Approach comparison

ApproachIdeaBest for
Nested int loops + ch--Outer width shrinks; print ch, then ch--Learning and interviews
putchar(ch)Write each letter without a format stringLeaner style after loops click

⚡ Quick Reference

GoalPattern
Walk widths downfor (i = rows; i >= 1; i--)
Start letterch = (char)('A' + rows - 1);
Print row letterprintf("%c", ch); — not j
End row + step letterprintf("\n"); then ch--;
putchar styleputchar(ch); then putchar('\n'); ch--;
Growing reverseSee Program 10 (E, DD, CCC, …)

📋 printf vs putchar

Same triangle — different ways to emit characters.

printf("%c", ch)
same line

Prints the row letter without moving to the next line

printf("\n")
new line

Ends the current row after all repeats are printed

putchar(ch)
leaner char I/O

Writes one character without a format string

Learning tip
print ch

Master the formula and nested loops before polishing with putchar

Context

When This Pattern Shows Up

Reach for this triangle when practicing inverted widths plus a per-row letter step.

  1. After Programs 9–10

    Invert the widths while letters still step E→A (or grow A→E in other variants).

  2. Outer vs inner roles

    Clear drill: outer = which letter, inner = how many copies.

  3. Char arithmetic practice

    Compute top = 'A' + rows - 1 and count down safely.

  4. Gateway to variants

    Lowercase, hollow borders, or centered pyramids next.

  5. Not a UI layout tool

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

Key benefit: one small program that separates letter choice from repeat count — the skill behind most alphabet patterns.

🔮 Live Preview

Choose a row count between 1 and 26 and draw the inverted repeating alphabet triangle in the browser.

Try 5 (EEEEE…A), 4 (DDDD…A), or 7. Max 26 keeps letters in A–Z.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — fixed inverted rows, scanf input, and a putchar shortcut. Click View Output to reveal sample console results.

📚 Getting Started

Print five inverted rows with classic nested loops and ch-- after each line.

Example 1 — Fixed 'E' down to 'A'

Hard-coded five inverted rows — ideal for first demos and screenshots.

c
#include <stdio.h>

int main() {
    int i, j;
    char ch = 'E';

    for (i = 5; i >= 1; --i) {
        for (j = 1; j <= i; ++j) {
            printf("%c", ch);
        }
        printf("\n");
        ch--;
    }

    return 0;
}

How It Works

When i = 5, the inner loop prints E five times. After the row, ch-- makes ch become 'D', then i = 4 prints DDDD, and so on until a single A. Printing ch (not j) keeps each row uniform.

📈 Practical Variant

Let the user choose the height at runtime.

Example 2 — User Input Version

Set ch = 'A' + rows - 1, then loop i from rows down to 1 with ch-- after each row. Check scanf in real apps.

c
#include <stdio.h>

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

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

    ch = (char)('A' + rows - 1);

    for (i = rows; i >= 1; --i) {
        for (j = 1; j <= i; ++j) {
            printf("%c", ch);
        }
        printf("\n");
        ch--;
    }

    return 0;
}

How It Works

For rows = 4, ch starts at 'D'. The first row prints DDDD, then CCC, and so on down to A. Clamp rows to 1–26 so the top letter stays within A–Z. Check scanf’s return value in safer labs.

⚡ Shortcut Style

Same shape using putchar instead of printf for each letter.

Example 3 — putchar(ch)

Write each letter with putchar, end the row with putchar('\n'), then ch--.

c
#include <stdio.h>

int main() {
    int i, j;
    char ch = 'E';

    for (i = 5; i >= 1; --i) {
        for (j = 1; j <= i; ++j) {
            putchar(ch);
        }
        putchar('\n');
        ch--;
    }

    return 0;
}

How It Works

putchar(ch) writes one character without a format string. Same inverted nested-loop structure as Example 1; a leaner alternative to printf("%c", ch). Keep either style for exams that want both loop bounds visible.

🧠 How the Algorithm Prints Rows

1

Set up

#include <stdio.h> brings in printf / scanf. Set ch = 'E' (or 'A' + rows - 1).

Setup
2

Outer loop (width)

for (i = rows; i >= 1; i--) selects the current width from wide to narrow.

5 → 1
3

Inner loop (count)

for (j = 1; j <= i; j++) prints ch with printf("%c", ch) exactly i times.

5..1
4

New line

printf("\n") ends the row; ch-- steps to the previous letter for the next shorter row.

Break + ch--
=

Triangle complete

Total letters: 1+2+…+n = n(n+1)/2O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — inverted five rows

Trace each outer-loop width i, the current letter ch, and how many times the inner loop runs.

ichPrinted rowRepeats
5'E'EEEEE5
4'D'DDDD4
3'C'CCC3
2'B'BB2
1'A'A1

Total letter prints: 5 + 4 + 3 + 2 + 1 = 15 = 5×6/2.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Outer vs Inner Clarity

Best demo that the printed value need not be the loop counter.

Example: swap printf("%c", ch) for something based on j and watch letters step.

2. Pair with Program 9

Teach direction as a one-line change: increment vs decrement.

Example: side-by-side A/BB/CCC vs E/DD/CCC.

3. Char Math Labs

Practice 'A' + rows - 1 without complex algorithms.

Example: rows = 7 → top = 'G'.

4. Case & Fill Variants

Swap to lowercase or mix digits once the loops work.

Example: start from 'a' + rows - 1.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed letters for n = 10 → 55.

6. Input Validation Labs

Pair the pattern with checked scanf and 1–26 clamps.

Example: reject rows <= 0 or rows > 26.

Pro Tip: when explaining this pattern, say “outer picks the letter, inner only counts” before writing any code — that story prevents printing j by mistake.

Advantages

Why this pattern earns a spot right after the forward repeating triangle.

  1. 1. Instant Visual Feedback

    Wrong printed variable shows up immediately as stepping letters.

  2. 2. Minimal Concepts

    Only loops, chars, and console output — no arrays required.

  3. 3. Easy to Mirror

    Flip to Program 9 by counting letters upward instead.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: learn the nested-loop printf version first; treat putchar as a polish shortcut afterward.

Usage Tips

Small habits that keep alphabet-pattern code clean.

  1. 1. Name the Roles

    Use ch for the row letter and repeat (or k) for the count — clearer than overloaded i/j.

  2. 2. Check scanf

    Verify scanf returns 1 so bad input does not leave rows unset.

  3. 3. Keep the Newline Outside

    Only call printf("\n") after the inner loop finishes the row.

  4. 4. Clamp to 26

    For A–Z demos, reject or clamp rows > 26.

  5. 5. Dry-Run One Small n

    Trace rows = 3 (C, BB, AAA) on paper before coding larger demos.

Pro Tip: if a row shows EDCBA-style sequences, you almost certainly printed j instead of i.

Common Pitfalls

Mistakes that commonly break inverted repeating alphabet patterns.

  1. 1. Printing j Instead of i

    Rows become countdown sequences instead of repeated letters.

    → Always printf("%c", ch) for this shape — not j. Put ch-- after the row, not inside the inner loop.

  2. 2. Newline Inside the Inner Loop

    Each letter lands on its own line — you get a column, not a triangle.

    → Use printf("%c", ch) for letters; printf("\n") only after the inner loop.

  3. 3. Forgetting the Row Break

    Omitting printf("\n") glues every letter onto one endless line.

    → Always end the row after the inner loop.

  4. 4. Unchecked scanf

    Letters or empty input leave rows uninitialized.

    → Check scanf’s return value and re-prompt on failure.

  5. 5. rows > 26 Without a Policy

    'A' + rows - 1 can leave the A–Z range.

    → Clamp to 26 or define wrap/error behavior explicitly.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single letter

Output is just A on one line.

rows = 0

Empty pattern

Treat as invalid; re-prompt instead of silent empty output.

Negative

rows < 0

Invalid height — validate before computing top.

rows > 26

Past Z

Clamp or error — char math leaves A–Z.

Bad input

Non-numeric input

Unchecked scanf fails silently — check the return value.

Case

Lowercase variant

Same loops work with 'a' and 'a' + rows - 1.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Compare with Program 9

  • Growing widths: A, BB, CCC, …
  • Continue with Program 9

2. Lowercase version

  • Use 'a' + rows - 1 as the top letter
  • Shows the loop structure is reusable

3. Safe input loop

  • Loop until scanf succeeds and 1 <= rows <= 26
  • Then draw the triangle

4. Hollow inverted triangle

  • Print the letter only on borders; spaces inside
  • Harder follow-up after this page

Notes

  • Triangular count. Total letters for n rows is n(n+1)/2 — hence O(n²) time.
  • Print the outer letter; the inner loop only decides how many times.
  • Validate 1 <= rows <= 26 for interactive A–Z programs.
  • This page is left-aligned. Centered pyramids need leading spaces — covered later in the series.

Quick Takeaway: outer loop picks the letter (counting down), inner loop repeats it, then break the line — that is the whole pattern.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
putchar (Example 3)O(rows²)O(1)
Wrap Up

🎉 Conclusion

The inverted repeating alphabet triangle is a small nested-loop exercise with lasting payoff: shrinking width vs letter step, ch-- after each row, and O(n²) intuition. Master the classic two-loop version, then optionally polish letter output with putchar.

Practice the three examples above, then compare with Programs 9–10 or continue to the next alphabet pattern.

Start ch at the top letter, shrink width with the outer loop, print with printf("%c", ch), end rows with printf("\n") then ch--, and clamp row counts to 1–26 when reading input.

💡 Best Practices

✅ Do

  • Explain outer = letter, inner = count before coding
  • Use printf("%c", ch) for letters and printf("\n") after each row
  • Validate 1 <= rows <= 26 for interactive programs
  • Check scanf’s return value instead of ignoring failed input
  • State O(n²) time when asked about complexity

❌ Don’t

  • Print the inner-loop variable for this repeating shape
  • Call printf("\n") inside the inner letter loop
  • Skip the newline after each row
  • Ignore bad console input in user-facing demos
  • Allow rows > 26 without a clear policy

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the inverted repeating triangle the beginner-friendly way.

5
Core concepts
02

Outer loop

Shrinks the row width

Code
A 03

Inner loop

Repeats with printf

Code
04

Newline + ch--

Ends row, steps letter

I/O
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

Program 10 prints widths 1..5 (E, DD, CCC, ...). Program 11 prints widths 5..1 (EEEEE, DDDD, ...). Both use repeating letters per row and letters counting down.
i starts at 5 so the first inner loop runs five times while ch is E. After the row, ch becomes D and i is 4, so the next line prints D four times.
So every column on a row shows the same letter. Decrementing inside the inner loop would step letters across the row.
j only controls how many times the loop runs. Printing ch keeps the entire row the same letter; printing j would step letters across the row.
printf("%c", ch) prints a letter and stays on the same line. printf("\n") ends the current line. Letters use %c; the row break uses \n after the inner loop.
O(n²) where n is the number of rows. Total letter prints equal n+(n-1)+…+1 = n(n+1)/2.
Yes. putchar(ch) writes one character without a format string. Nested loops with putchar are a common, slightly leaner style.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Then set ch = 'A' + rows - 1 and loop i from rows down to 1 with ch-- after each row. Clamp to 26 for A–Z demos.

Did you Know? 🔊

This is the inverted twin of Program 10 and the upside-down version of Program 9: letters still step E→A, but widths shrink 5, 4, 3, …, 1. Print ch in the inner loop, then ch-- after each row so every line stays uniform.

Continue to Alphabet Pattern 12

Keep building letter patterns with nested loops and char math.

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