A remove-last-digit number pattern prints an integer, then repeatedly strips the rightmost digit with integer division by 10 until the value reaches zero.
Remember
Rule: print num, then num = num / 10, until num == 0
86523
8652
865
86
8 ← start = 86523
Unlike Program 59 (nested grid loops), this pattern uses one while loop on a single number — a foundation for digit counting and reversals.
Approach
How to Solve It
Loop while the number is not zero: print the current value, then divide by 10 to drop the last digit.
Method
Idea
Best for
While + divide
Print, then num /= 10
Learning, interviews, demos
Compact trace
Same logic with num = 123
Quick dry-runs on paper
Pseudocode
Pseudocode
while num is not 0:
print num
num = num / 10
Cheat sheet
Goal
Pattern
Loop until empty
while (num != 0)
Print current value
cout << num << "\n";
Drop last digit
num = num / 10; or num /= 10;
Handle negatives
if (num < 0) num = -num;
Lines printed
Equals digit count of the starting number
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << num
Stays on the same line
The current value
cout << "\n" / cout << num << "\n"
Ends the line
Each shrinking step
Each step prints one complete number and ends the line (WriteLine-style).
Try it
Live Preview
Change the starting number and the shrinking pattern updates instantly.
Enter a positive integer up to 9 digits. Tap a chip or type a value — the preview redraws as you go.
Live result86523 · 5 lines
86523
8652
865
86
8
Trace
Worked Walkthrough
Trace num = 86523 — print first, then divide by 10.
Step
Prints
After num /= 10
1
86523
8652
2
8652
865
3
865
86
4
86
8
5
8
0 (loop ends)
Five lines for a five-digit start — each iteration removes exactly one digit.
Code
C++ Programs
Three complete programs: fixed 86523, cin input, and a compact 123 demo. Use View Output to reveal sample results.
Example 1 — Fixed num = 86523
Hard-coded start — print, divide by 10, repeat until zero.
C++
#include <iostream>
using namespace std;
int main()
{
int num = 86523;
while (num != 0)
{
cout << num << "\n";
num = num / 10;
}
return 0;
}
Output
86523
8652
865
86
8
How It Works
1. Guard the loop.while (num != 0) keeps going until every digit is stripped.
2. Print first. Output the current value before changing it, so every prefix appears.
3. Drop the last digit. Integer num / 10 discards the remainder — no floating point needed.
Example 2 — User Input Number
Read the starting value at runtime and normalize negatives before the loop.
C++
#include <iostream>
using namespace std;
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
if (num < 0)
num = -num;
while (num != 0)
{
cout << num << "\n";
num /= 10;
}
return 0;
}
Output (when user enters 9876)
Enter a number: 9876
9876
987
98
9
How It Works
1. Same loop core. Only the source of num changes from a literal to cin.
2. Negatives flip sign. Digit removal works on the magnitude, so -9876 shrinks like 9876.
3. Validate in real apps. Prefer checking cin failure before the loop (tip below).
Safer input tip
if (!(cin >> num))
{
cout << "Please enter a valid integer.\n";
return 1;
}
Example 3 — Compact num = 123
Three iterations — ideal for a paper dry-run before larger demos.
C++
#include <iostream>
using namespace std;
int main()
{
int num = 123;
while (num != 0)
{
cout << num << "\n";
num /= 10;
}
return 0;
}
Output
123
12
1
How It Works
1. Only three steps. Easy to dry-run print → divide on paper.
2. Same formula. Nothing changes except the starting value — proving the pattern scales.
3. Ends at 1. After printing 1, 1 / 10 becomes 0 and the loop stops.
Edge Cases & Pitfalls
Check these before calling the solution done.
Infinite loop
Always update num inside the loop
Forgetting num /= 10 leaves the condition true forever.
Zero start
num = 0 prints nothing
while (num != 0) never enters. Decide whether to print a single 0 for that case.
Trailing zeros
Trailing zeros disappear on the next line
120 becomes 12, then 1 — expected with integer division.
Bad cin
Validate input
Check cin >> num for failure before running the loop.
Analysis
Time and Space Complexity
Program
Time
Extra space
While + divide (Examples 1–3)
O(d)
O(1)
Each iteration removes one digit, so a d-digit number runs in linear time in d. Extra memory is only the loop variable.
Remember
Key Takeaways
Rule: print num, then num /= 10, until num == 0.
Integer division:/ 10 on int drops the last digit cleanly.
Print then shrink: output before dividing so every prefix appears.
Complexity:O(d) time, O(1) extra space.
One line: print the number, divide by 10, repeat until nothing remains.
Frequently Asked Questions
It prints the starting number on each line while removing the last digit each step. For 86523: 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.
You can print the number and the newline together with cout << num << "\n". That is the WriteLine-style end of each step.
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.