Rotating Number Pattern in C#

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:
12345
23451
34521
45321
54321Complete C# Program
First print numbers from i to rows, then print from i-1 down to 1.
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
Loop through rows
The outer loop sets the starting value i for each row.
Print i..rows
for (j = i; j <= rows; j++) prints the increasing part of the row.
Wrap back to 1
for (k = i; k > 1; k--) prints i-1 down to 1.
Rotating rows
Each row prints exactly rows digits, so total work is O(n²) for n rows.
Variation — User Input Rows
Let the user choose the row count. This version prints the same rotation style for any positive rows.
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
Row \(i\) prints \(i, i+1, \dots, n\) then \(i-1, \dots, 1\).
Two loops form a single row: a forward segment and a wrap segment.
Each row prints exactly \(n\) digits.
Total work is O(n²).
❓ Frequently Asked Questions
2 3 4 5, the wrap loop prints 1 (that’s i-1 when i = 2).Explore More C# Number Patterns!
Rotation-style patterns are great practice for loop boundaries and sequence design.
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.
12 people found this page helpful
