C# Number Pattern (Rotating Digits)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A rotating number pattern prints a fixed-width row that starts at i, climbs to rows, then wraps back down through i-1..1.

Remember
Rule: print i..rows, then (i-1)..1  (always rows digits)

12345
23451
34521
45321
54321   ← 5 rows

In C# use two inner loops: forward j from i to rows, then wrap k from i down to 2 printing k - 1.

How to Solve It

One outer loop and two inner loops that rotate the digit sequence.

MethodIdeaBest for
Forward + wrapPrint i..rows, then i-1..1Learning, interviews, exams
User-input rowsSame logic with a variable widthPractice / demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i to rows:
        print j
    for k from i down to 2:
        print (k - 1)
    print newline

Cheat sheet

GoalPattern
Next rotationfor (i = 1; i <= rows; i++)
Forward segmentfor (j = i; j <= rows; j++) Console.Write(j);
Wrap segmentfor (k = i; k > 1; k--) Console.Write(k - 1);
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit (no spaces)
Console.WriteLineEnds the current lineAfter both inner loops

Live Preview

Change the row count and the rotating pattern updates instantly — each row stays the same width.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 5 digits/row
12345
23451
34521
45321
54321

Worked Walkthrough — rows = 4

Trace the forward climb and the wrap-around. Every row has exactly 4 digits.

iForward / wrapPrinted row
11234 / (none)1234
2234 / 12341
334 / 213421
44 / 3214321

Forward length is rows - i + 1; wrap length is i - 1. Sum = rows.

C# Programs

Three complete programs: fixed height, user input, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed rows = 5

Forward loop prints i..rows; wrap loop prints i-1..1 via k - 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. Outer loop. i is the start digit of the rotation for that row.

2. Forward. Print from i up to rows — e.g. row 3 prints 345.

3. Wrap. Print k - 1 while k runs from i down to 2 — completing 34521.

Example 2 — User Input

Read the row count; each row still prints exactly rows digits.

C#
using System;

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

            Console.Write("Enter rows: ");
            if (!int.TryParse(Console.ReadLine(), out rows) || rows < 1)
            {
                Console.WriteLine("Please enter a positive whole number.");
                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();
            }
        }
    }
}

How It Works

1. Validate rows. TryParse rejects non-numeric input; require rows >= 1.

2. Same rotation. Three rows of width 3 end at 321.

Example 3 — Compact rows = 3

A smaller fixed demo — same forward-and-wrap idea, easier to trace by hand.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 3;
            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. Same rules. Forward prints i..rows; wrap prints i-1..1.

2. Quick check. The last row is a full reverse: 321.

Edge Cases & Pitfalls

Check these before calling the solution done.

print k

Print k instead of k - 1

The wrap would reprint i and skip 1. Always print k - 1.

wrong wrap end

Run wrap while k >= 1

That prints an extra 0. Keep k > 1.

WriteLine early

WriteLine between the two halves

That splits forward and wrap onto separate lines. Call WriteLine() only after both loops.

rows = 1

Single 1

Only the forward loop runs; the wrap loop never executes.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

There are n rows and each prints exactly n digits, so total work is n².

Key Takeaways

  • Rule: print i..rows, then i-1..1 — each row has exactly rows digits.
  • Wrap: use Console.Write(k - 1) while k runs from i down to 2.
  • Write vs WriteLine: digits stay on the line; WriteLine advances after both halves.
  • Next step: Program 40 prints an alternating 1 and 0 pattern.

One line: for each row i, print a forward climb to rows and a wrap back to 1 so the sequence rotates.

Frequently Asked Questions

For 5 rows: 12345, 23451, 34521, 45321, 54321 — each row starts at the row number and wraps back to 1.
The first loop prints i..rows (forward segment). The second loop prints i-1 down to 1 (wrap segment). Together they always produce rows digits.
After printing 2 3 4 5, the wrap loop prints k-1 when k runs from i down to 2 — for i=2 that prints 1.
Exactly rows digits every time — (rows - i + 1) forward plus (i - 1) wrap = rows.
Program 37 builds palindrome rows (i..2 then 1..i). Program 39 rotates: i..rows then i-1..1.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because each row prints n digits.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single digit 1.

Did you know?

Each row starts at i, prints i..rows, then wraps with i-1..1. Row i always prints exactly rows digits — total digits = n².

Next: Alternating 1 and 0 Pattern

Print rows of ones and zeros that shrink and alternate each line.

Program 40 tutorial →

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.

6 people found this page helpful