Right-Aligned Reverse Pyramid in C#

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):
A
BA
CBA
DCBA
EDCBAComplete 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.
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
Outer loop chooses the row peak
i moves from A to E, increasing row length each time.
Inner loop scans the full width
j runs from E down to A, giving a fixed-width line of 5 columns.
Spaces create right alignment
If j > i print a space; otherwise print j. Leading spaces push the suffix to the right edge.
Fixed width, growing suffix
For an alphabet span of n letters, the total work is O(n²).
Variation — User Input (Top Letter)
Let the user choose the last letter (like E). The pattern keeps the line width fixed to that letter.
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
ABCinstead ofCBA - Letting input exceed
Zwithout handling it
Key Takeaways
Fixed-width scan keeps rows aligned.
Condition j > i decides space vs letter.
Descending j prints suffix as CBA, DCBA, etc.
Overall work is O(n²) for n rows.
❓ Frequently Asked Questions
j > i. That pushes the letters to the right and forms a right-aligned pyramid.H and the inner scan starts from H down to A.12 people found this page helpful
