Rotating Number Pattern in C#

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

What You’ll Learn

How to print a pattern where each row starts from the row number and goes up to rows, then wraps back to 1.

It’s a neat way to practice combining two loops to form a single row sequence.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
12345
23451
34521
45321
54321
1

Complete C# Program

First print numbers from i to rows, then print from i-1 down to 1.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;
            int i, j, k;

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j <= rows; j++)
                    Console.Write(j);

                for (k = i; k > 1; k--)
                    Console.Write(k - 1);

                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Loop through rows

The outer loop sets the starting value i for each row.

Row control
2

Print i..rows

for (j = i; j <= rows; j++) prints the increasing part of the row.

First segment
3

Wrap back to 1

for (k = i; k > 1; k--) prints i-1 down to 1.

Second segment
=

Rotating rows

Each row prints exactly rows digits, so total work is O(n²) for n rows.

2

Variation — User Input Rows

Let the user choose the row count. This version prints the same rotation style for any positive rows.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            int i, j, k;

            Console.Write("Enter number of rows: ");
            if (!int.TryParse(Console.ReadLine(), out rows) || rows <= 0)
            {
                Console.WriteLine("Please enter a positive integer.");
                return;
            }

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j <= rows; j++)
                    Console.Write(j);

                for (k = i; k > 1; k--)
                    Console.Write(k - 1);

                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Add a space between digits for readability (print " " + j)
  • Print the wrap-around part as ascending instead of descending
  • Use characters (A..E) to make an alphabet rotation pattern

Avoid

  • Resetting loop counters incorrectly (keep segments separate)
  • Using rows \(\le 0\) without validation

Key Takeaways

1

Row \(i\) prints \(i, i+1, \dots, n\) then \(i-1, \dots, 1\).

2

Two loops form a single row: a forward segment and a wrap segment.

3

Each row prints exactly \(n\) digits.

4

Total work is O(n²).

❓ Frequently Asked Questions

Because after printing 2 3 4 5, the wrap loop prints 1 (that’s i-1 when i = 2).
Yes, but you’ll need to adjust the loop bounds and the wrap logic accordingly.

Explore More C# Number Patterns!

Rotation-style patterns are great practice for loop boundaries and sequence design.

All Number Patterns →
Did you know?

Splitting a row into two segments is a common technique in pattern problems: one segment prints the forward part, and another prints a wrap or mirror part.

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