Reverse Alphabet, Diagonal *

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

What You’ll Learn

Five rows, each like EDCBA but with exactly one * where the row letter meets the column letter (i == j).

The star slides from the right (EDCB*) to the left (*DCBA) as i increases. Compare program 16 (centered pyramid, no diagonal).

⭐ Pattern Output

Output
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA
1

Complete C Program (A–E)

Same logic as for (i = 65; i <= 69; i++) and j from 69 to 65.

c
#include <stdio.h>

int main() {
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Outer i picks the diagonal cell

i walks each row label from A up to E. That value is compared to the inner index j so exactly one position per row prints * instead of a letter.

Rows
2

Inner j scans the reverse alphabet

For each row, j counts from the top letter (E here) down to A. Without the star rule you would simply print the descending sequence across the row.

EDCBA
3

Diagonal swap: if (i == j)

When the row letter equals the column letter, emit * instead of printf("%c", j). That carves a diagonal of stars through the reverse-letter square.

Star
4

First row walkthrough

For i = 'A', j still scans EA. Letters print until j reaches 'A', which equals i, so that slot is *EDCB*.

Example
=

Full square

Five rows with five inner iterations each → 25 writes. For an n × n letter range the same pattern is O(n²).

2

Variation — User Input

endChar = 'A' + rows - 1; loops use startChar and endChar.

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 (i == j) {
                printf("*");
            } else {
                printf("%c", j);
            }
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Swap * for # or another marker
  • Mirror the inner loop to get ABCDE with a diagonal hole
  • Cap rows so endChar <= 'Z'

Avoid

  • Comparing i and j as different types without promotion rules in mind (here both behave as int in expressions)
  • Using rows larger than 26 without changing the alphabet range

Key Takeaways

1

Descending j gives reverse letters on each line.

2

i == j picks one diagonal cell per row.

3

Generalizes by setting endChar from rows.

4

O(n²) for an n×n letter square.

❓ Frequently Asked Questions

In the printed grid (rows top-to-bottom, columns left-to-right as j counts down), the stars sit on one anti-diagonal of the 5×5 letter matrix.
You can; reusing letter codes as indices keeps the condition a single equality check.
Use a relation like i + j == 'A' + 'E' (adjust ends for your range) so the match moves along the opposite slant.
O(n²) for n rows when each row has n columns.

Explore More C Alphabet Patterns!

Pairing indices with character codes is a compact way to mark one cell per row.

All Alphabet Patterns →
Did you know?

Here i and j are both promoted as integers in comparisons and printf("%c", j); the pattern still works if you declare them as char in C as long as you stay within a single-byte range.

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