Growing Reverse Number Pattern in C#

What You’ll Learn
How to print a growing reverse-number pattern in C#. Starting from a number like 86523, you’ll build its reverse step-by-step and print the intermediate results.
This is a nice way to practice modulo (%) and integer division (/) together.
⭐ Pattern Output
For num = 86523, the pattern looks like this:
3
32
325
3256
32568Complete C# Program
Build the reversed number one digit at a time and print it after each step.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int num = 86523;
int reverse = 0;
while (num != 0)
{
reverse = reverse * 10;
reverse = reverse + (num % 10);
Console.WriteLine(reverse);
num = num / 10;
}
}
}
}🧠 How It Works
Initialize num and reverse
num is the source number, and reverse starts at 0.
Extract the last digit
num % 10 gives the current last digit (3, then 2, then 5, …).
Append it to reverse
Multiply by 10 to shift left, then add the digit: reverse = reverse * 10 + digit.
Remove the last digit from num
num = num / 10 drops the last digit so the loop moves to the next one.
Growing reverse pattern
The loop runs once per digit, so runtime is O(d) for d digits.
Variation — User Input Number
Let the user provide the number. This version uses long for a bit more range and handles negative input.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number: ");
if (!long.TryParse(Console.ReadLine(), out long num))
{
Console.WriteLine("Please enter a valid integer.");
return;
}
num = Math.Abs(num);
long reverse = 0;
while (num != 0)
{
reverse = reverse * 10 + (num % 10);
Console.WriteLine(reverse);
num /= 10;
}
}
}
}💡 Tips for Enhancement
Try These
- Stop after a fixed number of digits (use a counter)
- Use
BigIntegerif you need to handle very large numbers - Also print the remaining
numto see both shrinking and growing values together - Collect intermediate values in a list and print them in reverse order
Avoid
- Using floating-point math (you want integer operations for digits)
- Forgetting to update
numinside the loop - Skipping input validation for user-provided numbers
Key Takeaways
num % 10 gets the last digit; num / 10 removes it.
reverse = reverse * 10 + digit appends a digit to the right.
You can print intermediate results to create a pattern from the reverse process.
Runtime is O(d) where d is the number of digits.
❓ Frequently Asked Questions
86523 is 3, so reverse becomes 3 on the first iteration.Explore More C# Number Patterns!
Digit-based patterns are great practice for modulo, division, and loop control.
Reversing numbers digit-by-digit is a classic interview problem and also the base idea behind palindrome checks and digit extraction techniques.
12 people found this page helpful
