Growing Reverse Number Pattern in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
While Loop + Modulo

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:

Output
3
32
325
3256
32568
1

Complete C# Program

Build the reversed number one digit at a time and print it after each step.

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

1

Initialize num and reverse

num is the source number, and reverse starts at 0.

Setup
2

Extract the last digit

num % 10 gives the current last digit (3, then 2, then 5, …).

Digit
3

Append it to reverse

Multiply by 10 to shift left, then add the digit: reverse = reverse * 10 + digit.

Build
4

Remove the last digit from num

num = num / 10 drops the last digit so the loop moves to the next one.

Shrink
=

Growing reverse pattern

The loop runs once per digit, so runtime is O(d) for d digits.

2

Variation — User Input Number

Let the user provide the number. This version uses long for a bit more range and handles negative input.

C#
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 BigInteger if you need to handle very large numbers
  • Also print the remaining num to 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 num inside the loop
  • Skipping input validation for user-provided numbers

Key Takeaways

1

num % 10 gets the last digit; num / 10 removes it.

2

reverse = reverse * 10 + digit appends a digit to the right.

3

You can print intermediate results to create a pattern from the reverse process.

4

Runtime is O(d) where d is the number of digits.

❓ Frequently Asked Questions

Because the first extracted digit from 86523 is 3, so reverse becomes 3 on the first iteration.
The final printed value is the full reverse. The earlier lines are partial reverses built along the way.
Trailing zeros in the original number are removed early by division. If you need to preserve them, you must handle the number as a string instead of an integer.
O(d), where d is the number of digits.

Explore More C# Number Patterns!

Digit-based patterns are great practice for modulo, division, and loop control.

All Number Patterns →
Did you know?

Reversing numbers digit-by-digit is a classic interview problem and also the base idea behind palindrome checks and digit extraction techniques.

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