Right-Aligned Reverse Pyramid in C

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

What You’ll Learn

Each row is a reverse suffix A, BA, CBA, … padded on the left so the letters line up on the right in a column of width five.

Same j > i idea as the left half of program 19, without the second mirror loop.

⭐ Pattern Output

Monospace (leading spaces matter):

Output
    A
   BA
  CBA
 DCBA
EDCBA
1

Complete C Program ('A''E')

Outer i is the row peak; inner j sweeps the full alphabet slice once per row.

c
#include <stdio.h>

int main() {
    int i, j;

    for (i = 'A'; i <= 'E'; ++i) {
        for (j = 'E'; j >= 'A'; --j) {
            if (j > i) {
                printf(" ");
            } else {
                printf("%c", j);
            }
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Outer i sets the visible tail

i runs AE. On each row, letters from i down to A will eventually be printed right-aligned; values above i are suppressed as spaces first.

Peak
2

Inner j scans 'E''A'

Every row performs the same five-column scan. That keeps the grid width fixed so right alignment is easy: you always decide “space or letter” at each column index.

Scan
3

Leading spaces when j > i

If the column letter j is still “above” the row’s start letter i, that letter has not appeared on this row yet, so you print a space. Padding shrinks as i moves toward E.

Pad
4

Letters when j <= i

Once j reaches the row threshold, each step prints the descending letter j. You get a contiguous suffix i, i-1, … A hugging the right margin.

Print
=

Grid cost

Five columns × five rows = 25 character decisions. For a span of n letters you scan n positions on each of n rows → O(n²).

2

Variation — User Input

endChar replaces 'E' in the inner loop bound.

c
#include <stdio.h>

int main() {
    int rows;
    int i, j;
    char startChar, endChar;

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

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

    for (i = startChar; i <= endChar; ++i) {
        for (j = endChar; j >= startChar; --j) {
            if (j > i) {
                printf(" ");
            } else {
                printf("%c", j);
            }
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Separate loop: print endChar - i spaces, then a simple j from i down to startChar
  • Flip to left-aligned forward triangle by scanning j upward
  • Use a monospace font in the terminal so columns line up

Avoid

  • j >= i for the space test — that turns the peak column into a space and drops the widest letter on the row
  • Mixing different endChar in outer vs inner loops

Key Takeaways

1

Descending j over a fixed range yields reverse order plus padding.

2

j > i prints spaces; j <= i prints letters.

3

Leading space count is endChar - i.

4

O(n²) for n rows with width n.

❓ Frequently Asked Questions

Program 11 repeats one letter per row. Here each row lists distinct descending letters from i to A, only shifted right.
The bottom row prints the full slice E through A; shorter rows pad on the left inside that same width.
That would treat the peak column as space when j == i and skip printing the row’s widest letter. Stick to j > i for this shape.
O(n²) for n rows.

Explore More C Alphabet Patterns!

Right alignment is “print the padding, then the payload” inside a fixed column count.

All Alphabet Patterns →
Did you know?

This is the same geometry as printing a left-justified reverse triangle, then shifting every line right by endChar - i spaces.

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