Shape Rule
Print then shrink
Each line shows the current num; the next line drops the rightmost digit — 86523 → 8652 → 865.

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.
Print then shrink
Each line shows the current num; the next line drops the rightmost digit — 86523 → 8652 → 865.
num !== 0
while (num !== 0) repeats until Math.floor(num / 10) reduces the value to zero.
num / 10
num = Math.floor(num / 10) drops the last digit — JavaScript uses Math.floor for integer truncation.
One line per step
console.log(num) logs the current value before the division step.
Any integer
Enter a starting number and watch the digit-removal pattern in the browser.
Complexity
One iteration per digit — 86523 has five lines; total steps equal digit count.
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).
Integer division and modulo are building blocks for digit counting, reversing numbers, palindrome checks, and sum-of-digits problems.
while (num !== 0) — one step per digit.
Integer division drops the last digit.
Program 60 shrinks the original; Program 61 builds a growing reverse.
Follow Program 59; continue to Program 61 next.
In short: while (num !== 0), console.log(num), then num = Math.floor(num / 10).
Given starting integer num = 86523, print the number on each line while removing the last digit until num becomes 0.
// num = 86523
//86523
//8652
//865
//86
//8 | Item | Type | Description |
|---|---|---|
num | number | Starting integer — updated each loop iteration. |
| Loop condition | bool | num !== 0 — stops when all digits are removed. |
| Print step | void | console.log(num) before dividing. |
| Update step | number | num = Math.floor(num / 10) drops the last digit. |
| Line count | number | Equals digit count of the starting number (86523 → 5 lines). |
| Final value | number | Loop ends at 0 — zero is not printed with != 0. |
while num is not 0:
log num
num = floor(num / 10) | Approach | Idea | Best for |
|---|---|---|
| while (num !== 0) | Print then divide by 10 | Standard digit-removal pattern |
| abs first | Handle negative input safely | User-input programs |
| Compact trace | num = 123 on paper first | Quick dry-runs |
| Track removed digit | num % 10 before dividing | Extension exercises |
BigInt | Arbitrary-precision integers | Very large starting values |
| Goal | Pattern |
|---|---|
| Loop | while (num !== 0) |
| Log | console.log(num) |
| Remove digit | num = Math.floor(num / 10) |
| Negative input | num = Math.abs(num) before the loop |
Same digit-removal pattern — three ways to set the starting number and trace the logic.
let num = 86523;Hard-coded start for demos
parseInt(prompt())Read starting number from console
let num = 123;Quick dry-run on paper
while !== 0One iteration per digit
Math.floor(num/10)Drop last digit each step
Reach for this pattern when teaching while loops, integer division, and digit manipulation in JavaScript.
Natural follow-up after grid patterns — switch from nested loops to a single while loop.
Foundation for counting digits, reversing numbers, and palindrome checks.
Classic while-loop question — explain print-then-divide before coding.
Compare shrinking the original with building a growing reverse number.
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.
Enter a positive integer between 10 and 99999999 and draw the digit-removal pattern in the browser.
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.
Print the digit-removal pattern for a hard-coded starting integer with a while loop.
num = 86523Hard-coded start — ideal for first demos and screenshots.
let num = 86523;
while (num !== 0) {
console.log(num);
num = Math.floor(num / 10);
} Print 86523, divide to get 8652, repeat until num becomes 0. The loop runs once per digit — five lines for a five-digit start.
Read the starting number from the user and handle negatives with abs.
Read num with prompt(), validate with Number.isFinite, and use Math.abs for negatives.
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);
}
} Same loop core as Example 1; only the source of num changes from a literal to user input, with validation and abs.
Use num = 123 for a quick paper trace before larger demos.
num = 123Same while loop with a smaller starting value — easy to dry-run on paper.
let num = 123;
while (num !== 0) {
console.log(num);
num = Math.floor(num / 10);
} Three iterations: print 123, then 12, then 1 — trace this small case before scaling to larger numbers.
Set num to a fixed value or read it with parseInt(prompt()).
while (num !== 0) keeps running until Math.floor(num / 10) reduces the value to zero.
console.log(num) outputs the current number on its own line.
num = Math.floor(num / 10) drops the rightmost digit.
One line per digit removed — O(d) time for d digits, O(1) extra memory.
num = 86523Trace each loop iteration: print the current num, then apply integer division by 10.
| Step | After Math.floor(num / 10) | Digits left | |
|---|---|---|---|
| 1 | 86523 | 8652 | 4 |
| 2 | 8652 | 865 | 3 |
| 3 | 865 | 86 | 2 |
| 4 | 86 | 8 | 1 |
| 5 | 8 | 0 (loop ends) | 0 |
Total lines printed: 5 = digit count of the starting number.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Step count depends on input — a natural introduction to condition-driven loops.
Example: trace num = 123 and count three iterations.
Foundation for counting digits, reversing numbers, and palindrome checks.
Example: track num % 10 before dividing to print removed digits.
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.
Shrinking the original pairs naturally with building a growing reverse number.
Example: Program 61 prints 3, 32, 325 from the same starting value.
One iteration per digit — makes O(d) concrete for beginners.
Example: num = 1000000 prints seven lines — seven digits.
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.
Why this pattern earns a permanent spot in beginner JavaScript courses.
Wrong loop updates show up immediately as an infinite loop or missing lines.
Only a while loop and integer division — no arrays or math libraries.
Track removed digits, include zero, or switch to modulo-based reverse builds with small edits.
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.
Small habits that keep digit-removal code clean.
Use num (or n) for the shrinking value.
Avoid crashes when the user types letters instead of a number.
Call console.log(num) before num = Math.floor(num / 10) so each step shows the current value.
Math.floor(num / 10) drops the last digit — use Math.floor, not plain / alone when you need integers.
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.
Mistakes that commonly break digit-removal patterns.
Without num = Math.floor(num / 10), the loop condition never changes — infinite loop.
→ Always divide by 10 after printing the current value.
Dividing doubles by 10 can introduce decimals — not what you want for digit stripping.
→ Keep num as int and use integer division.
while (num !== 0) never enters the body — silent empty output.
→ Validate input is non-zero or handle zero as a special case.
Letters or empty input return NaN when parseInt(prompt()) is unchecked.
→ Validate with Number.isFinite and re-prompt on failure.
Negative num still divides correctly but may confuse beginners reading output.
→ Apply Math.abs(num) before the loop when reading user input.
Check these inputs before calling the solution done.
while (num !== 0) never runs — log nothing or show a message.
One line prints 1, then loop ends — simplest case.
120 prints 120, 12, 1 — the trailing 0 vanishes on first division.
Apply abs before the loop — see Example 2.
Bare parseInt(prompt()) returns NaN — validate with Number.isFinite.
JavaScript integers have arbitrary precision — no overflow for typical inputs.
Try these variations to lock in the pattern.
num % 10 on its own linenum = 123 before coding/ 10 removes exactly one digit.num = 1 prints a single line.Quick Takeaway: while (num !== 0), log num, then num = Math.floor(num / 10) — that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| While loop (Examples 1–2) | O(d) — d = digit count | O(1) |
| Compact trace (Example 3) | O(d) | O(1) |
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().
while (num !== 0) with num = Math.floor(num / 10) inside the bodyconsole.log(num) before each division stepNumber.isFinite after parseInt(prompt()) for user inputnum inside the loop// floor division, not / float divisionabsnum = 123 dry-run before larger demosPrint the shrinking number pattern the beginner-friendly way.
Print num then divide by 10
DefinitionRuns until num is 0
CodeMath.floor(num / 10) drops last digit
MathOne line per step
I/OO(d) time
AnalysisEach iteration prints the current number, then num = Math.floor(num / 10) drops the last digit — runtime is O(d) for d digits.
Move on to the next pattern in the JavaScript number-pattern series.
12 people found this page helpful