Remove Last Digit Number Pattern in C#

Beginner
⏱️ 10 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
While Loop + Integer Division

What You’ll Learn

Program 60 prints a shrinking number pattern: start with an integer, print it, then remove the last digit with num / 10 until the value reaches zero. This tutorial covers the while-loop core, integer division, a live preview, worked C# examples, edge cases, and O(d) complexity.

Shape Rule

Print then shrink

Each line shows the current num; the next line drops the rightmost digit — 86523 → 8652 → 865.

While Loop

num != 0

while (num != 0) repeats until integer division reduces the value to zero.

Integer Division

num / 10

num = num / 10 (or num /= 10) drops the last digit — no floating-point needed.

WriteLine

One line per step

Console.WriteLine(num) prints the current value before the division step.

Live Preview

Any integer

Enter a starting number and watch the digit-removal pattern in the browser.

O(d)

Complexity

One iteration per digit — 86523 has five lines; total steps equal digit count.

Introduction

A remove-last-digit number pattern prints an integer, then repeatedly strips the rightmost digit until nothing remains. With num = 86523, you get 86523, 8652, 865, 86, 8.

In C# use while (num != 0), print with Console.WriteLine(num), then update with num = num / 10.

Why it matters?

Integer division and modulo are building blocks for digit counting, reversing numbers, palindrome checks, and sum-of-digits problems.

Key Highlights

While loop

while (num != 0) — one step per digit.

Divide by 10

Integer division drops the last digit.

vs Program 61

Program 60 shrinks the original; Program 61 builds a growing reverse.

Series Foundation

Follow Program 59; continue to Program 61 next.

In short: while (num != 0), WriteLine(num), then num /= 10.

📝 Problem & Approach

Given starting integer num = 86523, print the number on each line while removing the last digit until num becomes 0.

C#
// num = 86523
//86523
//8652
//865
//86
//8

Inputs & Outputs

ItemTypeDescription
numintStarting integer — updated each loop iteration.
Loop conditionboolnum != 0 — stops when all digits are removed.
Print stepvoidConsole.WriteLine(num) before dividing.
Update stepintnum = num / 10 drops the last digit.
Line countintEquals digit count of the starting number (86523 → 5 lines).
Final valueintLoop ends at 0 — zero is not printed with != 0.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
while (num != 0)Print then divide by 10Standard digit-removal pattern
Math.Abs firstHandle negative input safelyUser-input programs
Compact tracenum = 123 on paper firstQuick dry-runs
Track removed digitnum % 10 before dividingExtension exercises
long / BigIntegerWider integer typesVery large starting values

⚡ Quick Reference

GoalPattern
Loopwhile (num != 0) { ... }
PrintConsole.WriteLine(num);
Remove digitnum = num / 10; or num /= 10;
Negative inputnum = Math.Abs(num); before the loop

📋 Fixed Value vs User Input vs Compact Trace

Same digit-removal pattern — three ways to set the starting number and trace the logic.

Fixed value
num = 86523

Hard-coded start for demos

User input
TryParse

Read starting number from console

Compact trace
num = 123

Quick dry-run on paper

Loop
while != 0

One iteration per digit

Update
num /= 10

Drop last digit each step

Context

When This Pattern Shows Up

Reach for this pattern when teaching while loops, integer division, and digit manipulation in C#.

  1. Post Program 59 exercise

    Natural follow-up after grid patterns — switch from nested loops to a single while loop.

  2. Digit manipulation drills

    Foundation for counting digits, reversing numbers, and palindrome checks.

  3. Interview warm-ups

    Classic while-loop question — explain print-then-divide before coding.

  4. Gateway to Program 61

    Compare shrinking the original with building a growing reverse number.

  5. Not for huge numbers only

    Use long or BigInteger when inputs exceed int range.

Key benefit: one tiny program that locks in while loops, integer division, and O(d) thinking.

🔮 Live Preview

Enter a positive integer between 10 and 99999999 and draw the digit-removal pattern in the browser.

Try 123, 86523, or 1200. Max 8 digits in this preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C# programs — fixed starting value, user input with negative handling, and a compact num = 123 trace. Click View Output to reveal sample console results.

📚 Getting Started

Print the digit-removal pattern for a hard-coded starting integer with a while loop.

Example 1 — Fixed num = 86523

Hard-coded start — ideal for first demos and screenshots.

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

Print 86523, divide to get 8652, repeat until num becomes 0. The loop runs once per digit — five lines for a five-digit start.

📈 Practical Variant

Read the starting number from the user and handle negatives with Math.Abs.

Example 2 — User Input Version

Read num from the console with safe parsing and absolute value for negatives.

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;
            }
        }
    }
}

How It Works

Same loop core as Example 1; only the source of num changes from a literal to user input, with validation and Math.Abs.

⚡ Compact Trace

Use num = 123 for a quick paper trace before larger demos.

Example 3 — Compact num = 123

Same while loop with a smaller starting value — easy to dry-run on paper.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 123;

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

How It Works

Three iterations: print 123, then 12, then 1 — trace this small case before scaling to larger numbers.

🧠 How the Algorithm Removes Digits

1

Set up

using System; brings in Console. Set num (fixed or from input).

Setup
2

While loop

while (num != 0) keeps running until integer division reduces the value to zero.

Loop
3

Print current value

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

Output
4

Remove last digit

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

Math
=

Digit-removal pattern complete

One line per digit removed — O(d) time for d digits, O(1) extra memory.

🔎 Worked Walkthrough — num = 86523

Trace each loop iteration: print the current num, then apply integer division by 10.

StepPrintAfter num /= 10Digits left
18652386524
286528653
3865862
48681
580 (loop ends)0

Total lines printed: 5 = digit count of the starting number.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching While Loops

Step count depends on input — a natural introduction to condition-driven loops.

Example: trace num = 123 and count three iterations.

2. Digit Manipulation

Foundation for counting digits, reversing numbers, and palindrome checks.

Example: track num % 10 before dividing to print removed digits.

3. Integer Division Practice

/ 10 on integers drops the last digit — concrete arithmetic, not abstract math.

Example: compare 86523 / 10 with floating-point division.

4. Gateway to Program 61

Shrinking the original pairs naturally with building a growing reverse number.

Example: Program 61 prints 3, 32, 325 from the same starting value.

5. Complexity Intuition

One iteration per digit — makes O(d) concrete for beginners.

Example: num = 1000000 prints seven lines — seven digits.

6. Input Validation Labs

Pair the pattern with TryParse and zero-checks.

Example: reject non-numeric input and handle num = 0.

Pro Tip: when an interviewer asks for digit patterns, explain print-then-divide before writing the loop — the story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner C# courses.

  1. 1. Instant Visual Feedback

    Wrong loop updates show up immediately as an infinite loop or missing lines.

  2. 2. Minimal Concepts

    Only a while loop and integer division — no arrays or math libraries.

  3. 3. Easy to Extend

    Track removed digits, include zero, or switch to modulo-based reverse builds with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond the loop variable.

Pro Tip: trace num on paper for 123 before coding — watch how each division drops one digit.

Usage Tips

Small habits that keep digit-removal code clean.

  1. 1. Name the Variable Clearly

    Use num (or n) for the shrinking value.

  2. 2. Prefer TryParse

    Avoid crashes when the user types letters instead of a number.

  3. 3. Print Before Dividing

    Call WriteLine(num) before num /= 10 so each step shows the current value.

  4. 4. Use Integer Division

    num / 10 on integers drops the last digit — avoid floating-point division.

  5. 5. Dry-Run One Small Number

    Trace num = 123 on paper before coding larger demos.

Pro Tip: if the loop never stops, you almost certainly forgot num /= 10 inside the body.

Common Pitfalls

Mistakes that commonly break digit-removal patterns.

  1. 1. Forgetting to Update num

    Without num /= 10, the loop condition never changes — infinite loop.

    → Always divide by 10 after printing the current value.

  2. 2. Using Floating-Point Division

    Dividing doubles by 10 can introduce decimals — not what you want for digit stripping.

    → Keep num as int and use integer division.

  3. 3. Starting with num = 0

    while (num != 0) never enters the body — silent empty output.

    → Validate input is non-zero or handle zero as a special case.

  4. 4. Blind Convert.ToInt32

    Letters or empty input throw FormatException.

    → Prefer int.TryParse and re-prompt on failure.

  5. 5. Negative Numbers Without Abs

    Negative num still divides correctly but may confuse beginners reading output.

    → Apply Math.Abs(num) before the loop when reading user input.

Edge Cases

Check these inputs before calling the solution done.

num = 0

Zero input

while (num != 0) never runs — print nothing or show a message.

num = 1

Single digit

One line prints 1, then loop ends — simplest case.

120

Trailing zero

120 prints 120, 12, 1 — the trailing 0 vanishes on first division.

Negative

Negative input

Apply Math.Abs before the loop — see Example 2.

Bad input

Non-numeric ReadLine

Convert.ToInt32 throws — use TryParse.

Large n

Large numbers

Use long or BigInteger when inputs exceed int range.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Print removed digits

  • Before dividing, print num % 10 on its own line
  • See which digit disappears each step

2. Compare with Program 61

  • Program 60 shrinks the original number
  • Program 61 builds a growing reverse pattern

3. Next in series

  • Continue with Program 61
  • Build on the same while-loop skills

4. Paper trace

  • Dry-run num = 123 before coding
  • Fill the walkthrough table by hand

Notes

  • Line count. Total lines equal the digit count of the starting number — hence O(d) time.
  • Integer division discards the remainder — that is why / 10 removes exactly one digit.
  • Validate non-zero input for interactive programs; num = 1 prints a single line.
  • To include the final zero, adjust the loop condition or print zero after the last division.

Quick Takeaway: while (num != 0), print num, then num /= 10 — that is the whole pattern.

⏱️ Time and Space Complexity

ProgramTimeExtra space
While loop (Examples 1–2)O(d) — d = digit countO(1)
Compact trace (Example 3)O(d)O(1)
Wrap Up

🎉 Conclusion

The remove-last-digit number pattern is a small while-loop exercise with lasting payoff: integer division, digit manipulation, and O(d) intuition. Master the fixed-value version, then try user input and the compact num = 123 trace.

Practice the three examples above, then continue to Program 61 for the next pattern in the series.

Print before dividing — keep num as an integer, and validate input when reading from the console.

💡 Best Practices

✅ Do

  • Explain print-then-divide before coding
  • Use while (num != 0) with num /= 10 inside the body
  • Use Console.WriteLine(num) before each division step
  • Validate non-zero input for interactive programs
  • Prefer int.TryParse over bare Convert.ToInt32
  • State O(d) time when asked about complexity

❌ Don’t

  • Forget to update num inside the loop
  • Use floating-point division instead of integer division
  • Skip input validation when reading from the user
  • Assume negative input works without Math.Abs
  • Skip the num = 123 dry-run before larger demos

Key Takeaways

Knowledge Unlocked

Five things to remember about this digit-removal pattern

Print the shrinking number pattern the beginner-friendly way.

5
Core concepts
02

While loop

Runs until num is 0

Code
10 03

Division

num /= 10 drops last digit

Math
04

WriteLine

One line per step

I/O
O 05

Complexity

O(d) time

Analysis

❓ 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, then print with the sign if needed — see Example 2.
No. while (num != 0) skips the body entirely when the starting value is already 0.
O(d) where d is the number of digits — each iteration removes exactly one digit.
Prefer int.TryParse(Console.ReadLine(), out num) so bad input does not throw FormatException.

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.

Continue to Program 61

Move on to the next pattern in the C# number-pattern series.

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