Odd-Length Alphabet Triangle in C++

What You’ll Learn
Each row is a block of letters from A through the next “odd step” in the alphabet: A, ABC, ABCDE, ABCDEFG, ABCDEFGHI.
The outer loop uses i += 2 (values A, C, E, G, I); compare program 13, where width grows by one each row and a separate counter drives the letters.
⭐ Pattern Output
Five rows, no spaces between letters:
A
ABC
ABCDE
ABCDEFG
ABCDEFGHIComplete C++ Program (Classic Codetofun Form)
Outer i steps by 2 from 65 while i <= 74 (same effect as i <= 73 or i <= 'I'). Inner j prints every code from 65 through i.
#include <iostream>
using namespace std;
int main() {
int i, j;
for (i = 65; i <= 74; i += 2) {
for (j = 65; j <= i; j++)
cout << (char) j;
cout << "\n";
}
return 0;
}🧠 How It Works
i += 2
Outer loop visits A, C, E, G, I — every other capital letter.
Inner j from 65 to i
Always rebuilds the prefix from the start of the alphabet up to the current end letter.
cout << (char) j
Emits each letter in that range; no separate running counter is needed.
New line per outer step
After finishing one prefix, move to the next wider odd-length row.
25 characters total
1+3+5+7+9 = 25; for r rows this is r² prints.
Variation — Ending Letter from Input
User supplies the last outer value (odd-position letter from A: A, C, E, …). Use cin >> ws before cin >> endChar if prior reads might leave whitespace.
#include <iostream>
using namespace std;
int main() {
int i, j;
char endChar;
cout << "Enter the ending letter (e.g. E for A,C,E rows): ";
cin >> ws >> endChar;
for (i = 'A'; i <= endChar; i += 2) {
for (j = 'A'; j <= i; j++)
cout << (char) j;
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Clearer bound:
for (i = 'A'; i <= 'I'; i += 2)with innerjfrom'A'toi - Even-end variant: start at
'B'and step by 2 for B, D, F, … - Validate
endCharif you need a predictable ladder shape
Avoid
- Mixing up “odd length” with “odd ASCII code” — here it is odd count per row (1, 3, 5, …)
- Reading
endCharwithout skipping whitespace when the buffer still has a newline
Key Takeaways
i += 2 picks every other letter for the row end marker.
Inner loop always runs 'A'..i, so widths grow by 2 each row.
Total prints for r such rows: r² (here 5² = 25).
Prefer i <= 'I' over a loose numeric bound when you want five rows through I.
❓ Frequently Asked Questions
i = 73 ('I'). Using i <= 73 or i <= 'I' states the intent more clearly.i = 'A' (since 'C' would exceed B). For a full ladder through B, you would change the loop design.Explore More C++ Alphabet Patterns!
Changing the outer step size reshapes how fast each row grows—compare program 1’s step-by-one width with this odd-width ladder.
Row lengths 1, 3, 5, …, (2r−1) sum to r². So five rows always print exactly 25 characters, same as a 5×5 block of letters would if you filled it cleverly.
12 people found this page helpful
