An odd-length alphabet triangle reprints A..end on every row, while end jumps by two letters each time — so widths are always 1, 3, 5, 7, …
Remember
Rule: end letter steps A, C, E, …; each row prints A..end
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI ← 5 rows (ends A,C,E,G,I)
Unlike Program 13 (one running cursor across the whole triangle), every row here restarts at A and grows to a farther odd-step ending letter. Compare with Program 1, where the end advances by one letter each row.
Approach
How to Solve It
Step the end letter by two, then print the fresh prefix A..end on each row.
Method
Idea
Best for
Char step += 2
Outer i walks A, C, E, …; inner prints A..i
Learning, interviews, fixed demos
Row index
end = 'A' + 2*(row-1)
When the user enters a row count
Pseudocode
Pseudocode
for i from 'A' to lastEnd step 2:
for j from 'A' to i:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Step the end letter
for (char i = 'A'; i <= 'I'; i += 2)
Print the prefix
for (char j = 'A'; j <= i; j++) cout << j;
End the row
cout << "\n";
Row-count form
end = (char)('A' + 2 * (row - 1));
Spaced letters
cout << j << ' ';
A–Y-safe height
Max 13 rows (end reaches Y)
Printing Letters vs Starting a New Line
API
Effect
Use for
cout << j
Stays on the same line
Each letter on the row
cout << "\n"
Ends the current line
After the inner loop
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the odd-length triangle updates instantly — including end letter and letter totals.
Whole numbers from 1 to 13. Row r ends at letter A + 2*(r-1) (A–Y).
Live result5 rows · ends I · 25 letters
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
Trace
Worked Walkthrough — rows = 4
Trace each end letter and the prefix printed from A through that end.
Row
End i
Inner j
Printed row
Count
1
A
A..A
A
1
2
C
A..C
ABC
3
3
E
A..E
ABCDE
5
4
G
A..G
ABCDEFG
7
Total letters: 1 + 3 + 5 + 7 = 16 = 4². That square sum is why time is O(r²).
Code
C++ Programs
Three complete programs: fixed through I, ending-letter input with cin, and a row-count form. Use View Output to reveal sample results.
Example 1 — Fixed through 'I'
Hard-coded ending letter — ideal for first demos and screenshots.
C++
#include <iostream>
using namespace std;
int main() {
char i, j;
for (i = 'A'; i <= 'I'; i += 2) {
for (j = 'A'; j <= i; j++) {
cout << j;
}
cout << "\n";
}
return 0;
}
Output
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
How It Works
1. Outer loop steps the end.i takes A, C, E, G, I via i += 2.
2. Inner loop restarts at A. For each i, print every letter from A through i.
3. Break the line.cout << "\n" after the inner loop starts the next longer prefix.
In C++, i += 2 on a char is fine — the value promotes for arithmetic, then assigns back.
Example 2 — Ending Letter Input
Read an odd-step ending letter (A, C, E, …). Prefer validating a single A–Y character in real apps.
C++
#include <iostream>
using namespace std;
int main() {
char end, i, j;
cout << "Enter the ending letter (odd step like I): ";
cin >> end;
for (i = 'A'; i <= end; i += 2) {
for (j = 'A'; j <= i; j++) {
cout << j;
}
cout << "\n";
}
return 0;
}
Output (when user enters E)
Enter the ending letter (odd step like I): E
A
ABC
ABCDE
How It Works
1. Prompt and read.cin >> end stores one character as the outer upper bound.
2. Same nested-loop core. Only the outer upper bound changes — the print logic matches Example 1.
3. Safer input tip. Prefer a single uppercase odd-step letter:
Safer input
char end;
if (!(cin >> end) || end < 'A' || end > 'Y' || ((end - 'A') % 2) != 0) {
cout << "Enter one odd-step letter A, C, E, …, Y.\n";
return 1;
}
Example 3 — end = 'A' + 2*(row-1)
Drive the pattern from a row count instead of an ending letter.
C++
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int row;
char end, j;
for (row = 1; row <= rows; row++) {
end = (char)('A' + 2 * (row - 1));
for (j = 'A'; j <= end; j++) {
cout << j;
}
cout << "\n";
}
return 0;
}
Output
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
How It Works
1. Map row → end. Row 1 ends at A+0, row 2 at A+2, row 3 at A+4, and so on.
2. Print the prefix. Inner loop still walks A..end on every row.
3. Stay in A–Y. Clamp rows to 1–13 so end never walks past Y.
Edge Cases & Pitfalls
Check these before calling the solution done.
i++
Even lengths
If the outer loop uses i++ instead of i += 2, you get A, AB, ABC, … — not the odd-length pattern.
Inner starts at i
Missing prefix
Always start the inner loop at 'A'. Starting at i prints a single letter per row.
cout "\n" inside
Column of letters
If cout << "\n" sits inside the inner loop, each letter lands on its own line. End the row only after the inner loop.
Even end letter
Messy last row
Prefer odd-step endings (A, C, E, …, Y). An even letter like D still runs but breaks the clean odd-width story.
rows = 1
Single A
Output is just A — a good sanity check.
Past Y
Clamp to 13 rows
Row 13 ends at Y. Larger values walk past Z — clamp or stop early.
Analysis
Time and Space Complexity
Program
Time
Extra space
Char step / row-count forms
O(r²)
O(1)
Total letters printed = 1 + 3 + … + (2r - 1) = r², which is quadratic in the number of rows.
Remember
Key Takeaways
Odd ends: outer letter steps A, C, E, … with i += 2.
Fresh prefix: every row prints A..end from scratch.
Break the row:cout << j for letters; cout << "\n" after the inner loop.
Complexity:O(r²) time because total letters equal r²; O(1) extra space.
One line: for each end letter stepping by two, print A..end, then cout << "\n".
Frequently Asked Questions
It makes the ending letter jump by two (A, C, E, ...) so each row length increases by two characters and stays odd.
The outer loop advances the ending letter by 2 (A, C, E, G, I). The inner loop prints every letter from A through that ending letter, so the count is always odd.
Because each row is a fresh prefix A..end. Starting at the end letter would skip earlier letters and change the pattern.
cout << ch stays on the same line. cout << "\n" ends the current line. Letters use cout << j; the row break uses cout << "\n" after the inner loop.
1+3+…+(2r-1)=r². For 5 rows that is 25 letters.
O(r²) for r rows, because total letter prints equal r².
The loop still runs, but you no longer get a clean set of odd-length rows aligned to A, C, E, …. Prefer an odd-step ending letter (A, C, E, …, Y) for this pattern.
After cin >> end or cin >> rows, check failure with if (!(cin >> …)). Prefer an odd-step letter A–Y, or clamp rows to 1–13 so the end stays within A–Y.
🤔
Did you know?
Odd numbers add up to perfect squares: 1+3+5+…+(2r-1)=r². That is why this pattern prints exactly r² letters for r rows — the same count that makes the complexity O(r²).