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 fixed-width column.

This is the same j > i idea as the left half of Program 19, but 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 E down to A and prints a space until it reaches i.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            for (char i = 'A'; i <= 'E'; i++)
            {
                for (char j = 'E'; j >= 'A'; j--)
                {
                    if (j > i)
                        Console.Write(" ");
                    else
                        Console.Write(j);
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Outer loop chooses the row peak

i moves from A to E, increasing row length each time.

Rows
2

Inner loop scans the full width

j runs from E down to A, giving a fixed-width line of 5 columns.

Width
3

Spaces create right alignment

If j > i print a space; otherwise print j. Leading spaces push the suffix to the right edge.

Align
=

Fixed width, growing suffix

For an alphabet span of n letters, the total work is O(n²).

2

Variation — User Input (Top Letter)

Let the user choose the last letter (like E). The pattern keeps the line width fixed to that letter.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the top letter (like E): ");
            char top = Convert.ToChar(Console.ReadLine());

            for (char i = 'A'; i <= top; i++)
            {
                for (char j = top; j >= 'A'; j--)
                {
                    Console.Write(j > i ? ' ' : j);
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Replace spaces with . to visualize the alignment while debugging
  • Print spaces between letters for a wider look
  • Center the whole line by adding an extra left padding
  • Compare with Program 19 (adds the right mirror half)

Avoid

  • Using tabs for indentation (alignment varies by environment)
  • Flipping the inner loop direction unless you want ABC instead of CBA
  • Letting input exceed Z without handling it

Key Takeaways

1

Fixed-width scan keeps rows aligned.

2

Condition j > i decides space vs letter.

3

Descending j prints suffix as CBA, DCBA, etc.

4

Overall work is O(n²) for n rows.

❓ Frequently Asked Questions

Because we print leading spaces for columns where j > i. That pushes the letters to the right and forms a right-aligned pyramid.
Update the loop bounds so the outer loop runs up to H and the inner scan starts from H down to A.
O(n²) because there are n rows and each row scans n positions.

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