C# Number Pattern (Remove Last Digit)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A remove-last-digit pattern prints an integer, then repeatedly strips the rightmost digit with integer division by 10 until the value becomes zero.

Remember
Rule: while num != 0 → print num → num = num / 10

86523
8652
865
86
8     ← start = 86523

Follows the hollow square border in Program 59; next is the growing reverse pattern in Program 61.

How to Solve It

Use a while loop: print the current value, then divide by 10. Stop when num reaches 0.

MethodIdeaBest for
while + divide by 10Print, then num /= 10 until zeroLearning, interviews, digit drills
Abs for negativesnum = Math.Abs(num) before the loopUser input that may be negative

Pseudocode

Pseudocode
while num is not 0:
    print num
    num = num / 10

Cheat sheet

GoalPattern
Loop conditionwhile (num != 0)
Print currentConsole.WriteLine(num);
Drop last digitnum = num / 10; or num /= 10;
Handle negativesnum = Math.Abs(num);
Line countEquals the digit count of the starting number

Write vs WriteLine

APIEffectUse for
Console.Write(num)Stays on the same lineRare here — values would glue together
Console.WriteLine(num)Prints the value and ends the lineEach step of the pattern

Unlike nested triangle patterns, each iteration needs its own newline — so use WriteLine(num) every step.

Live Preview

Change the starting number and the digit-removal pattern updates instantly — absolute value, up to 9 digits.

Whole numbers from 1 to 999999999 (negatives use absolute value). Tap a chip or type a value.

Live result num = 86523 · 5 lines
86523
8652
865
86
8

Worked Walkthrough — num = 86523

Trace each print-then-divide step until the loop stops.

StepPrintsAfter / 10
1865238652
28652865
386586
4868
580 (loop ends)

Five digits → five lines → O(d) time. Integer division truncates toward zero, so no floating-point is involved.

C# Programs

Three complete programs: fixed num = 86523, TryParse input, and a compact num = 123 demo. Use View Output to reveal sample results.

Example 1 — Fixed num = 86523

Hard-coded start — print, then divide by 10 until zero.

C#
using System;

class Program
{
    static void Main()
    {
        int num = 86523;

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

How It Works

1. Print first. Show the current num before changing it.

2. Divide by 10. Integer division drops the last digit — 86523 / 10 is 8652.

3. Stop at zero. When num becomes 0, while (num != 0) ends.

Example 2 — User Input Number

Read num with int.TryParse, take absolute value, then run the same loop.

C#
using System;

class Program
{
    static void Main()
    {
        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;
        }
    }
}

How It Works

1. Prompt and validate. Prefer TryParse so bad input does not throw.

2. Absolutize. Math.Abs(num) keeps the digit-strip logic positive (and avoids sign quirks).

3. Same core. Print-then-divide matches Example 1 — only the source of num changes.

Example 3 — Compact num = 123

Three lines only — easy to dry-run on paper before larger demos.

C#
using System;

class Program
{
    static void Main()
    {
        int num = 123;

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

How It Works

1. Three digits. 123 → 12 → 1 → 0 — three prints, then stop.

2. Trace on paper. Confirm you print before dividing; reverse that order and you miss the first line.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for 86523 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

divide first

Missing the first line

If you divide before printing, the starting number never appears. Always print, then num /= 10.

num = 0

Empty output

while (num != 0) skips entirely when the start is already 0 — no lines are printed.

trailing 0

Zeros disappear next step

120 prints 120, then 12, then 1 — the trailing zero is not a separate line after division.

negatives

Sign without Abs

Without Math.Abs, negative starts print with a minus and still shrink toward zero — use Abs when you want unsigned digit stripping.

Write only

All on one line

If you use Write instead of WriteLine, every value glues together on one line.

TryParse

Validate input

Prefer int.TryParse before the loop — bad input should not throw FormatException.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(d)O(1)
Compact num = 123 (Example 3)O(d)O(1)

Each iteration removes one digit, so the loop runs d times for a d-digit start — linear in digit count, constant extra memory.

Key Takeaways

  • Print then divide: show num, then num /= 10.
  • while until zero: digit count is unknown ahead of time — while (num != 0) fits naturally.
  • Integer division: / 10 truncates — that is how the last digit disappears.
  • Complexity: O(d) time for d digits; O(1) extra space.

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

Frequently Asked Questions

It prints the starting number on each line while removing the last digit each step. For 86523, output is 86523, 8652, 865, 86, 8.
Integer division by 10 discards the remainder: 86523/10 becomes 8652, then 865, then 86, then 8.
The number of steps equals the digit count — unknown until you read the input. while (num != 0) keeps going until all digits are stripped.
Program 59 uses nested loops on a grid. Program 60 uses one while loop and integer division on a single number.
Program 60 shrinks the original number by dividing by 10. Program 61 builds a growing reverse number using modulo and division.
A trailing 0 is removed on the next step — 120 becomes 12, then 1.
Apply Math.Abs(num) before the loop — see Example 2.
O(d) where d is the number of digits — each iteration removes exactly one digit.

Did you know?

Each iteration prints the current number, then num = num / 10 drops the last digit using integer division — runtime is O(d) for d digits.

Next: Growing Reverse Number Pattern

Continue by building a reverse number digit-by-digit with modulo and division.

Program 61 tutorial →

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