Remove Last Digit Number Pattern in JavaScript

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 Math.floor(num / 10) until the value reaches zero. This tutorial covers the while-loop core, integer division, a live preview, worked JavaScript 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 Math.floor(num / 10) reduces the value to zero.

Math.floor

num / 10

num = Math.floor(num / 10) drops the last digit — JavaScript uses Math.floor for integer truncation.

console.log()

One line per step

console.log(num) logs 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 JavaScript use while (num !== 0), log with console.log(num), then update with num = Math.floor(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), console.log(num), then num = Math.floor(num / 10).

📝 Problem & Approach

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

JavaScript
// num = 86523
//86523
//8652
//865
//86
//8

Inputs & Outputs

ItemTypeDescription
numnumberStarting integer — updated each loop iteration.
Loop conditionboolnum !== 0 — stops when all digits are removed.
Print stepvoidconsole.log(num) before dividing.
Update stepnumbernum = Math.floor(num / 10) drops the last digit.
Line countnumberEquals digit count of the starting number (86523 → 5 lines).
Final valuenumberLoop ends at 0 — zero is not printed with != 0.

Minimal workflow

Pseudocode
while num is not 0:
    log num
    num = floor(num / 10)

Approach comparison

ApproachIdeaBest for
while (num !== 0)Print then divide by 10Standard digit-removal pattern
abs firstHandle negative input safelyUser-input programs
Compact tracenum = 123 on paper firstQuick dry-runs
Track removed digitnum % 10 before dividingExtension exercises
BigIntArbitrary-precision integersVery large starting values

⚡ Quick Reference

GoalPattern
Loopwhile (num !== 0)
Logconsole.log(num)
Remove digitnum = Math.floor(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
let num = 86523;

Hard-coded start for demos

User input
parseInt(prompt())

Read starting number from console

Compact trace
let num = 123;

Quick dry-run on paper

Loop
while !== 0

One iteration per digit

Update
Math.floor(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 JavaScript.

  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 JavaScript’s arbitrary-precision integers when inputs exceed typical 32-bit limits.

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 JavaScript 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.

JavaScript
let num = 86523;

while (num !== 0) {
  console.log(num);
  num = Math.floor(num / 10);
}
Try it Yourself

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 abs.

Example 2 — User Input Version

Read num with prompt(), validate with Number.isFinite, and use Math.abs for negatives.

JavaScript
const numInput = prompt("Enter a number:");
const parsed = parseInt(numInput, 10);

if (!Number.isFinite(parsed)) {
  console.log("Please enter a valid integer.");
} else {
  let num = Math.abs(parsed);

  while (num !== 0) {
    console.log(num);
    num = Math.floor(num / 10);
  }
}
Try it Yourself

How It Works

Same loop core as Example 1; only the source of num changes from a literal to user input, with validation and 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.

JavaScript
let num = 123;

while (num !== 0) {
  console.log(num);
  num = Math.floor(num / 10);
}
Try it Yourself

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

Set num to a fixed value or read it with parseInt(prompt()).

Setup
2

While loop

while (num !== 0) keeps running until Math.floor(num / 10) reduces the value to zero.

Loop
3

Print current value

console.log(num) outputs the current number on its own line.

Output
4

Remove last digit

num = Math.floor(num / 10) drops 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 Math.floor(num / 10)Digits 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

Math.floor(num / 10) drops the last digit — compare with plain num / 10 which keeps decimals.

Example: compare Math.floor(86523 / 10) with 86523 / 10.

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 parseInt(prompt()) 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 JavaScript 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. Validate User Input

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

  3. 3. Print Before Dividing

    Call console.log(num) before num = Math.floor(num / 10) so each step shows the current value.

  4. 4. Use Floor Division

    Math.floor(num / 10) drops the last digit — use Math.floor, not plain / alone when you need integers.

  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 = Math.floor(num / 10) inside the body.

Common Pitfalls

Mistakes that commonly break digit-removal patterns.

  1. 1. Forgetting to Update num

    Without num = Math.floor(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. Unchecked parseInt(prompt())

    Letters or empty input return NaN when parseInt(prompt()) is unchecked.

    → Validate with Number.isFinite 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 — log 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 abs before the loop — see Example 2.

Bad input

Non-numeric input

Bare parseInt(prompt()) returns NaN — validate with Number.isFinite.

Large n

Large numbers

JavaScript integers have arbitrary precision — no overflow for typical inputs.

🎯 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), log num, then num = Math.floor(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.

Log before dividing — keep num as an integer, and validate input when reading from prompt().

💡 Best Practices

✅ Do

  • Explain log-then-divide before coding
  • Use while (num !== 0) with num = Math.floor(num / 10) inside the body
  • Use console.log(num) before each division step
  • Validate non-zero input for interactive programs
  • Use Number.isFinite after parseInt(prompt()) for user input
  • State O(d) time when asked about complexity

❌ Don’t

  • Forget to update num inside the loop
  • Use // floor division, not / float division
  • Skip input validation when reading from the user
  • Assume negative input works without 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

Math.floor(num / 10) drops last digit

Math
04

Newline

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.
Math.floor(num / 10) discards the remainder: Math.floor(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.
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.
Use parseInt with Number.isFinite after prompt(). Bare parseInt(prompt()) returns NaN on bad input.

Did you Know? 🔊

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

Continue to Program 61

Move on to the next pattern in the JavaScript 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