Remove Last Digit Number Pattern in C#

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
While Loop + Integer Division

What You’ll Learn

How to print a number pattern by repeatedly removing the last digit of an integer in C#.

You’ll print the current number, then update it using num = num / 10 until it becomes 0.

⭐ Pattern Output

For num = 86523, the pattern looks like this:

Output
86523\n8652\n865\n86\n8
1

Complete C# Program

Use a while loop and integer division by 10 to strip digits from the right.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 86523;
            while (num != 0)
            {
                Console.WriteLine(num);
                num = num / 10;
            }
        }
    }
}

🧠 How It Works

1

Start with an integer

int num = 86523; is the starting value we’ll print and shrink.

Setup
2

Loop until the number becomes 0

while (num != 0) keeps printing until all digits are removed.

Loop
3

Print the current value

Console.WriteLine(num) outputs the current number on its own line.

Output
4

Remove the last digit

num = num / 10; uses integer division to drop the last digit.

Math
=

Shrinking number pattern

You print one line per digit removed, so runtime is O(d) for d digits.

2

Variation — User Input Number

Read the starting number from the user. This version also handles negative inputs by converting to absolute value.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter a number: ");
            if (!int.TryParse(Console.ReadLine(), out int num))
            {
                Console.WriteLine("Please enter a valid integer.");
                return;
            }

            num = Math.Abs(num);

            while (num != 0)
            {
                Console.WriteLine(num);
                num /= 10;
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Stop after printing a specific number of lines (use a counter)
  • Use long or BigInteger for very large values
  • Print removed digits too by tracking num % 10 before dividing
  • Build a reverse pattern by storing intermediate values in a list and printing back

Avoid

  • Using floating-point division (you want integer division for digit removal)
  • Forgetting to update num inside the loop (it would become infinite)
  • Skipping input validation when reading from the user

Key Takeaways

1

Integer division by 10 removes the last digit of a number.

2

A while loop is a natural fit when the number of steps depends on the digits.

3

You can safely support negative inputs with Math.Abs.

4

Runtime is proportional to the number of digits.

❓ Frequently Asked Questions

Because integer division discards the remainder. So 86523 / 10 becomes 8652.
No. The condition while (num != 0) prevents printing when the number is already 0.
Print once before the loop, then loop while num >= 10, or adjust the condition so you print 0 after the last division.
O(d) where d is the number of digits in the number.

Explore More C# Number Patterns!

Mix loops and simple math to create surprisingly many number patterns.

All Number Patterns →
Did you know?

Removing digits with integer division is commonly used in problems like digit counting, reversing numbers, checking palindromes, and computing digit sums.

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