Increasing-Decreasing Number Pyramid in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
1
232
34543
4567654
567898765
1

Complete C# Program

We print the increasing part first, then step back and print the decreasing part.

C#
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

1

Choose the row count

rows controls how many lines are printed.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) prints one row at a time and sets the starting number to i.

Row control
3

Increasing part (left to peak)

m starts at i and we print it i times, incrementing after each print.

Increase
4

Decreasing part (back to start)

We step back with m = m - 2 to avoid repeating the peak, then print i-1 numbers while decrementing.

Decrease
=

Palindromic rows

Row i prints \(2i-1\) digits, so total work is on the order of O(n²) for n rows.

2

Variation — User Input Version

Let the user choose the number of rows at runtime:

C#
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 rows to generate a larger pattern
  • Print the pattern with a fixed width using string formatting

Avoid

  • Forgetting the m = m - 2 adjustment (it prevents repeating the peak)
  • Using negative or zero rows without validating input
  • Mixing row logic and print logic (keep loops focused)

Key Takeaways

1

Each row prints an increasing sequence followed by a decreasing sequence.

2

m = m - 2 avoids printing the peak number twice.

3

Row i prints \(2i-1\) digits, so total output grows like \(n^2\).

4

The same technique can be adapted to alphabets and other mirrored patterns.

❓ Frequently Asked Questions

Row 3 starts at 3, prints up to 5, then prints back down to 3, producing 34543.
After the increasing loop, 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.
Yes. Print with a trailing space like Console.Write(m++ + " ") and adjust the decreasing loop similarly.
O(n²) for n rows, since total printed digits are \(1+3+5+\dots+(2n-1)=n^2\).

Explore More C# Number Patterns!

Mirrored number pyramids like this one are perfect practice for loop control and sequence building.

All Number Patterns →
Did you know?

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\).

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