An inverted alphabet right-angled triangle prints the longest letter run first, then shrinks by one letter each row — still starting from A every time.
Remember
Rule: for end letter i from last down to A,
print A through i
ABCDE
ABCD
ABC
AB
A ← 5 rows (longest on top)
It is the flip of Program 1: keep the same inner A..i print, reverse only the outer loop. Compare with Program 4, which grows while printing backward, and Program 6, which shrinks from the left instead.
Approach
How to Solve It
Two ways to emit the same shape — nested char loops, or the same loops with spaced letters for readability.
Method
Idea
Best for
Nested char loops
Outer = shrinking end; inner = A..end
Learning, interviews, exams
Spaced letters
Same loops; print "%c " per letter
Easier column scanning once loops click
Pseudocode
Pseudocode
for end from lastLetter down to 'A':
for ch from 'A' to end:
print ch (no newline)
print newline
Change the row count and the inverted triangle updates instantly — longest row on top.
Whole number from 1 to 10. Tap a chip or type a count — the preview redraws as you go.
Live result5 rows · top E · 15 letters
ABCDE
ABCD
ABC
AB
A
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop end letter as i counts down from 'D' to 'A'.
End i
Inner j
Printed row
Letters
'D'
A..D
ABCD
4
'C'
A..C
ABC
3
'B'
A..B
AB
2
'A'
A..A
A
1
Total letter prints: 4 + 3 + 2 + 1 = 10 = 4×5/2 — same as Program 1, only the print order of rows differs.
Code
C Programs
Three complete programs: fixed top E, row-count input with scanf, and spaced letters. Use View Output to reveal sample results.
Example 1 — Fixed from 'E' down to 'A'
Hard-coded top letter — outer loop shrinks; inner loop still prints A..i.
c
#include <stdio.h>
int main(void) {
char i, j;
for (i = 'E'; i >= 'A'; i--) {
for (j = 'A'; j <= i; j++) {
printf("%c", j);
}
printf("\n");
}
return 0;
}
Output
ABCDE
ABCD
ABC
AB
A
How It Works
1. Outer loop picks the end letter.i runs from 'E' down to 'A' — longest row first.
2. Inner loop restarts at A. For each i, j runs from 'A' to i, so the row is A..i.
3. Print letters, then break the line.printf("%c", j) stays on the row; printf("\n") after the inner loop starts the next (shorter) row.
When i = 'E' you get ABCDE; when i = 'A' you get A.
Example 2 — Row Count Input
Read the row count, map it to a top letter, then shrink. Check scanf in real apps.
c
#include <stdio.h>
int main(void) {
int rows;
char i, j, top;
printf("Enter the number of rows: ");
scanf("%d", &rows);
top = 'A' + rows - 1;
for (i = top; i >= 'A'; i--) {
for (j = 'A'; j <= i; j++) {
printf("%c", j);
}
printf("\n");
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
ABCD
ABC
AB
A
How It Works
1. Map rows to a top letter.top = 'A' + rows - 1 — for rows = 4, top is 'D'.
2. Same shrink core. Only the source of top changes — the print logic matches Example 1.
3. Safer input tip. Prefer:
Safer input
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 26) {
printf("Enter a whole number from 1 to 26.\n");
return 1;
}
Example 3 — Spaced Letters
Print a trailing space after each letter so columns are easier to scan.
c
#include <stdio.h>
int main(void) {
char i, j;
for (i = 'E'; i >= 'A'; i--) {
for (j = 'A'; j <= i; j++) {
printf("%c ", j);
}
printf("\n");
}
return 0;
}
Output
A B C D E
A B C D
A B C
A B
A
How It Works
1. Same bounds. Loop limits are unchanged — only the printed unit becomes "%c ".
2. Optional trim. Trim trailing spaces later if you need a compact line for grading scripts.
Edge Cases & Pitfalls
Check these before calling the solution done.
i++
Growing triangle by mistake
If you increment i from 'A' to top, you reprint Program 1. Use i-- from top down to 'A'.
Stop at B
Missing final A
Stopping before 'A' drops the tip row. The condition must be i >= 'A'.
printf("\n") inside
Column of letters
If printf("\n") is inside the inner loop, each letter lands on its own line. Print letters with %c; break the line only after the row.
rows > 26
Past Z
'A' + rows - 1 leaves A–Z when rows > 26. Clamp or reject in interactive programs.
rows = 1
Single A
Output is just A — longest and shortest coincide. A good sanity check.
scanf
Check return value
scanf("%d", &rows) can fail on empty or non-numeric text. Check == 1 and validate the range.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(n²)
O(1)
Spaced letters (Example 3)
O(n²)
O(1)
Total letters = n + (n - 1) + … + 1 = n(n + 1)/2 — still quadratic in n. Same totals as Program 1.
Remember
Key Takeaways
Rule: countdown end letter i; print A through i each row.
Flip of Program 1: same inner loop — only reverse the outer loop.
Break the row: call printf("\n") only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: for i from the top letter down to 'A', print A through i, then printf("\n").
Frequently Asked Questions
The outer loop counts down so the first row prints the most letters and each next row prints one fewer. The inner loop still prints from A up to the current bound.
Because the inner loop always starts at 'A'. That resets the sequence each row, producing ABCDE then ABCD and so on.
Program 1 grows row length (A, AB, ABC). This pattern shrinks it (ABCDE, ABCD, ABC). Only the outer loop direction changes; the inner loop is the same A..i print.
Program 4 grows while printing backward (A, BA, CBA). This pattern shrinks while printing forward from A (ABCDE, ABCD, ABC).
printf("%c", j) 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²) for n rows, because total printed characters are n(n+1)/2.
Yes. Read rows with scanf, set top = (char)('A' + rows - 1), then loop i from top down to 'A' and print j from 'A' up to i.
Check scanf("%d", &rows) == 1, require n ≥ 1, and cap at 26 so the top letter stays within A–Z.
🤔
Did you know?
This is the inverted twin of Program 1: same inner A..i print, only the outer loop shrinks. For 5 rows: ABCDE, ABCD, ABC, AB, A. Total letters still equal n(n+1)/2.