Right-Aligned Sequential Pyramid in C#

What You’ll Learn
This pattern prints letters in a running sequence (A, then B C, then D E F…) but keeps the triangle right-aligned by printing empty cells first.
For best alignment, view output in a monospace terminal because we rely on fixed-width cells.
⭐ Pattern Output
Output for 5 rows:
A
B C
D E F
G H I J
K L M N OComplete C# Program (Fixed 5 Rows)
Direct adaptation of the reference logic: a single counter k increments only when a letter is printed, and {0,2} keeps columns aligned.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
char k = 'A';
for (int i = 1; i <= 5; i++)
{
for (int j = 5; j >= 1; j--)
{
if (j > i)
Console.Write(" ");
else
Console.Write("{0,2}", k++);
}
Console.WriteLine();
}
}
}
}🧠 How It Works
k = 'A'
A single running counter that never resets between rows.
Right alignment via empty cells
The inner scan runs from 5 down to 1. When j > i we print two spaces (" ") to keep the same cell width as a letter.
Fixed-width letter printing
We print letters using {0,2}, so each letter occupies 2 columns and lines up with the padding.
Sequence continues
Because k increments only when we print a letter, the alphabet continues across rows.
Variation — User Input Rows
Choose how many rows to print. Note: for large values, the letters will go past Z.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter number of rows (like 5): ");
int n = int.Parse(Console.ReadLine());
char k = 'A';
for (int i = 1; i <= n; i++)
{
for (int j = n; j >= 1; j--)
{
if (j > i)
Console.Write(" ");
else
Console.Write("{0,2}", k++);
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Print a space after each letter to get a more airy pyramid
- Reset
kevery row if you want rows likeA,A B,A B C - Use lowercase by starting at
'a' - Center the whole shape by adding leading padding
Avoid
- Viewing output in proportional fonts (alignment will look off)
- Printing one space for padding (cells become uneven)
- Letting
krun pastZwithout a wrap rule
Key Takeaways
k is a running sequence across rows.
Two spaces match the width of {0,2}.
Right alignment comes from printing empty cells first.
Total work is O(n²) for n rows.
❓ Frequently Asked Questions
{0,2}). Two spaces keeps empty cells aligned with printed cells.k.12 people found this page helpful
