Decreasing & Increasing Alphabet Rows in C++

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

Each row is made from two parts: a descending run from the row letter down to B, then an ascending run starting from A up to a computed cap so every row stays the same width.

The key formula is cap = startChar + endChar - i (which becomes 134 - i in the ASCII version for A.. E).

⭐ Pattern Output

Output for A.. E (no spaces):

Output
ABCDE
BABCD
CBABC
DCBAB
EDCBA
1

C++ Program (Reference Logic, ASCII bound)

This uses the same bound as the reference: k <= 134 - i.

C++
#include <iostream>
using namespace std;

int main() {
    int i, j, k;
    for (i = 65; i <= 69; i++) {
        for (j = i; j > 65; j--)
            cout << char(j);
        for (k = 65; k <= 134 - i; k++)
            cout << char(k);
        cout << "\n";
    }
    return 0;
}

🧠 How It Works

1

Outer i is the row anchor (ASCII)

i runs 6569 (AE). It is the leftmost high letter on the row; the second loop prints the ascending tail that keeps every line five characters wide.

Rows
2

Descending leg: j from i toward B

for (j = i; j > 65; --j) emits char(j) with cout. When i == 65 the condition fails immediately, so row one is only the ascending half.

Down
3

Ascending leg: k through 134 - i

for (k = 65; k <= 134 - i; ++k) is 65 + 69 - i in ASCII terms ('A' + 'E' - i). As i increases, the cap drops so the combined segments always print exactly five letters for this sample range.

Cap
4

Constant row length

Descending count is i - 65; ascending count is (134 - i) - 65 + 1 = 69 - i + 1. Their sum is 5 for every row.

5
=

Bow-tie pattern

Middle rows splice a descending prefix with an ascending tail. Two inner passes per row → O(n²) for n letters in the span.

2

Variation — User Input (no magic numbers)

Generalize using startChar and endChar so the cap becomes startChar + endChar - i.

C++
#include <iostream>
using namespace std;

int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;

    char startChar = 'A';
    char endChar = char('A' + rows - 1);

    for (char i = startChar; i <= endChar; ++i) {
        for (char j = i; j > startChar; --j) cout << j;
        for (char k = startChar; k <= char(startChar + endChar - i); ++k) cout << k;
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print a space after each character for readability
  • Derive the cap as endChar - (i - startChar) (equivalent form)
  • Validate rows to stay within A..Z

Avoid

  • Using 2 * endChar - i (overshoots on the first row)
  • Letting the descending loop print A (duplicates A at the join)

Key Takeaways

1

Cap formula: startChar + endChar - i (ASCII: 134 - i for A..E).

2

Descending loop stops before A to avoid duplicates.

3

Row length remains constant: endChar - startChar + 1.

4

O(n²) output for span n.

❓ Frequently Asked Questions

134 = 65 + 69 which is 'A' + 'E'. So 134 - i is just 'A' + 'E' - i.
Because A is printed by the second loop. This prevents printing A twice.
O(n²) for span n.

Explore More C++ Alphabet Patterns!

Formulas like start + end - i are a handy way to avoid hard-coded bounds when generating symmetric strings.

All Alphabet Patterns →
Did you know?

The second loop prints (A + E - i) - A + 1 = E - i + 1 letters, which perfectly complements the i - A letters from the first loop to keep every row length constant.

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