Remove Last Digit Number Pattern in C#

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:
86523\n8652\n865\n86\n8Complete C# Program
Use a while loop and integer division by 10 to strip digits from the right.
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
Start with an integer
int num = 86523; is the starting value we’ll print and shrink.
Loop until the number becomes 0
while (num != 0) keeps printing until all digits are removed.
Print the current value
Console.WriteLine(num) outputs the current number on its own line.
Remove the last digit
num = num / 10; uses integer division to drop the last digit.
Shrinking number pattern
You print one line per digit removed, so runtime is O(d) for d digits.
Variation — User Input Number
Read the starting number from the user. This version also handles negative inputs by converting to absolute value.
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
longorBigIntegerfor very large values - Print removed digits too by tracking
num % 10before 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
numinside the loop (it would become infinite) - Skipping input validation when reading from the user
Key Takeaways
Integer division by 10 removes the last digit of a number.
A while loop is a natural fit when the number of steps depends on the digits.
You can safely support negative inputs with Math.Abs.
Runtime is proportional to the number of digits.
❓ Frequently Asked Questions
86523 / 10 becomes 8652.while (num != 0) prevents printing when the number is already 0.num >= 10, or adjust the condition so you print 0 after the last division.Explore More C# Number Patterns!
Mix loops and simple math to create surprisingly many number patterns.
Removing digits with integer division is commonly used in problems like digit counting, reversing numbers, checking palindromes, and computing digit sums.
12 people found this page helpful
