Centered Alphabet Pyramid in C#

What You’ll Learn
Print a small centered pyramid: one letter on the first row, then 3 letters, then 5 letters, with leading spaces so it looks aligned.
Letters flow continuously via one counter (k): A, then B C D, then E F G H I.
⭐ Pattern Output
Three rows (spaces between letters; leading spaces align columns):
A
B C D
E F G H IComplete C# Program
Outer loop prints 1, 3, 5 letters. Inner loop scans 5 columns; it prints spaces first, then letters (with a trailing space) to keep the pyramid aligned.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int k = 0;
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (int i = 1; i <= 5; i += 2)
{
for (int j = 5; j >= 1; j--)
{
if (j > i)
{
Console.Write(" ");
}
else
{
Console.Write(alpha[k++] + " ");
}
}
Console.WriteLine();
}
}
}
}🧠 How It Works
One running counter
k starts at 0 and increments after every printed letter, so output stays sequential.
Odd row widths
Outer i runs 1, 3, 5. That determines how many letters appear on each row.
Leading spaces for centering
The inner loop checks j > i. Those positions print spaces so shorter rows are padded on the left.
Complexity
For r rows, you scan O(r) positions per row, so overall complexity is O(r²).
Variation — User Input (Odd Rows)
Read the bottom width as an odd number (like 7). The outer loop prints 1..width stepping by 2, and the inner loop scans a fixed width each row.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the bottom width (odd number): ");
int width = Convert.ToInt32(Console.ReadLine());
char k = 'A';
for (int i = 1; i <= width; i += 2)
{
for (int j = width; j >= 1; j--)
{
if (j > i)
{
Console.Write(" ");
}
else
{
Console.Write(k + " ");
k++;
}
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Print without trailing spaces by controlling separators (like in program 13)
- Use lowercase by starting with
k = 'a' - Cap output at
'Z'if you only want A–Z - Compare with Program 14 (odd-width without centering)
Avoid
- Even widths if you want symmetric odd rows (use odd widths like 5, 7, 9)
- Resetting the letter counter each row if you want continuous letters
- Forgetting padding spaces (rows will shift left)
Key Takeaways
Outer loop controls odd row widths (1, 3, 5...).
Inner loop prints spaces first to center the row.
One running counter prints consecutive letters.
Complexity is O(r²) for r rows.
❓ Frequently Asked Questions
k (or control separators carefully).Explore More C# Alphabet Patterns!
Once you can center output with spaces, you can build diamond and pyramid variations too.
This pattern combines two ideas: odd-width rows (i += 2) and centering via padding spaces (like star pyramids).
12 people found this page helpful
