Increasing-Decreasing Number Pyramid in C#

What You’ll Learn
How to print an increasing-decreasing number pyramid in C#.
On each row i, we print numbers starting from i up to the peak, then print back down to i to form a mirrored (palindromic) row.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
232
34543
4567654
567898765Complete C# Program
We print the increasing part first, then step back and print the decreasing part.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = 1; i <= rows; i++)
{
int m = i;
for (int j = 1; j <= i; j++)
Console.Write(m++);
m = m - 2;
for (int k = 1; k < i; k++)
Console.Write(m--);
Console.WriteLine();
}
}
}
}🧠 How It Works
Choose the row count
rows controls how many lines are printed.
Outer loop (rows)
for (i = 1; i <= rows; i++) prints one row at a time and sets the starting number to i.
Increasing part (left to peak)
m starts at i and we print it i times, incrementing after each print.
Decreasing part (back to start)
We step back with m = m - 2 to avoid repeating the peak, then print i-1 numbers while decrementing.
Palindromic rows
Row i prints \(2i-1\) digits, so total work is on the order of O(n²) for n rows.
Variation — User Input Version
Let the user choose the number of rows at runtime:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows: ");
if (!int.TryParse(Console.ReadLine(), out int rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (int i = 1; i <= rows; i++)
{
int m = i;
for (int j = 1; j <= i; j++)
Console.Write(m++);
m = m - 2;
for (int k = 1; k < i; k++)
Console.Write(m--);
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Add spacing between numbers for readability (e.g.,
Console.Write(m++ + " ")) - Right-align the pyramid using leading spaces
- Increase
rowsto generate a larger pattern - Print the pattern with a fixed width using string formatting
Avoid
- Forgetting the
m = m - 2adjustment (it prevents repeating the peak) - Using negative or zero rows without validating input
- Mixing row logic and print logic (keep loops focused)
Key Takeaways
Each row prints an increasing sequence followed by a decreasing sequence.
m = m - 2 avoids printing the peak number twice.
Row i prints \(2i-1\) digits, so total output grows like \(n^2\).
The same technique can be adapted to alphabets and other mirrored patterns.
❓ Frequently Asked Questions
3, prints up to 5, then prints back down to 3, producing 34543.m is one step past the peak. Subtracting 2 moves it to the value just before the peak so the decreasing loop doesn’t repeat the peak digit.Console.Write(m++ + " ") and adjust the decreasing loop similarly.Explore More C# Number Patterns!
Mirrored number pyramids like this one are perfect practice for loop control and sequence building.
The total digits printed in this pattern is \(n^2\) for \(n\) rows, because each row has \(2i-1\) digits and \(1+3+5+\dots+(2n-1)=n^2\).
12 people found this page helpful
