A growing reverse number pattern builds the reverse of an integer one digit at a time and prints each partial result — the companion to Program 60’s shrinking original.
Unlike Program 60 (print then shrink the original), this pattern extracts digits from the right and grows a new reverse value.
Approach
How to Solve It
Loop while the source is not zero: extract the last digit, append it to reverse, print, then shrink the source.
Method
Idea
Best for
While + modulo
Extract, append, print, shrink
Learning, interviews, demos
Compact trace
Same logic with num = 123
Quick dry-runs on paper
Pseudocode
Pseudocode
reverse = 0
while num is not 0:
digit = num % 10
reverse = reverse * 10 + digit
print reverse
num = num / 10
Cheat sheet
Goal
Pattern
Loop until empty
while (num != 0)
Extract last digit
int digit = num % 10;
Append to reverse
reverse = reverse * 10 + digit;
Print + shrink
cout << reverse << "\n"; num /= 10;
Initialize
int reverse = 0; before the loop
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << reverse
Stays on the same line
The partial reverse
cout << "\n" / cout << reverse << "\n"
Ends the line
Each growing step
Each step prints one complete partial reverse and ends the line (WriteLine-style).
Try it
Live Preview
Change the starting number and the growing reverse 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
3
32
325
3256
32568
Trace
Worked Walkthrough
Trace num = 86523 — extract, append, print, then shrink.
Step
num % 10
reverse after append
After num /= 10
1
3
3
8652
2
2
32
865
3
5
325
86
4
6
3256
8
5
8
32568
0 (loop ends)
Five lines for a five-digit start — the final reverse equals the fully reversed number.
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 — build reverse one digit at a time and print after each append.
C++
#include <iostream>
using namespace std;
int main()
{
int num = 86523;
int reverse = 0;
while (num != 0)
{
reverse = reverse * 10 + (num % 10);
cout << reverse << "\n";
num = num / 10;
}
return 0;
}
Output
3
32
325
3256
32568
How It Works
1. Extract the digit.num % 10 pulls the rightmost digit (3 from 86523).
2. Append and print.reverse * 10 + digit shifts left and adds the new digit, then print.
3. Shrink the source.num /= 10 drops the used digit so the next iteration advances.
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;
int reverse = 0;
cout << "Enter a number: ";
cin >> num;
if (num < 0)
num = -num;
while (num != 0)
{
reverse = reverse * 10 + (num % 10);
cout << reverse << "\n";
num /= 10;
}
return 0;
}
Output (when user enters 9876)
Enter a number: 9876
6
67
678
6789
How It Works
1. Same loop core. Only the source of num changes from a literal to cin.
2. Negatives flip sign. Digit work uses the magnitude, so -9876 grows like 9876.
3. Validate in real apps. Prefer checking cin failure, and use long long if reverse may overflow int (tip below).
Safer input tip
if (!(cin >> num))
{
cout << "Please enter a valid integer.\n";
return 1;
}
// For larger values: use long long for num and reverse.
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;
int reverse = 0;
while (num != 0)
{
reverse = reverse * 10 + (num % 10);
cout << reverse << "\n";
num /= 10;
}
return 0;
}
Output
3
32
321
How It Works
1. Only three steps. Easy to dry-run extract → append → print on paper.
2. Same formula. Nothing changes except the starting value — proving the pattern scales.
3. Full reverse at the end. The last line 321 is the complete reverse of 123.
Edge Cases & Pitfalls
Check these before calling the solution done.
Missing *10
Always multiply before adding
Without reverse * 10, you get single digits (3, 2, 5) instead of (3, 32, 325).
Trailing zero
A trailing 0 prints first
120 yields 0, then 2, then 21 — expected when extracting from the right.
Overflow
Watch int limits
Large inputs can overflow when building reverse — use long long for bigger demos.
Zero start
num = 0 prints nothing
while (num != 0) never enters. Decide whether to print a single 0 for that case.
Analysis
Time and Space Complexity
Program
Time
Extra space
While + modulo (Examples 1–3)
O(d)
O(1)
The loop runs once per digit — linear in the digit count d. Extra memory is only num, reverse, and a digit variable.
Remember
Key Takeaways
Rule:reverse = reverse * 10 + (num % 10), print, then num /= 10.
Modulo:num % 10 extracts the current last digit.
Shift left: multiply by 10 before appending, or reverse stays single digits.
Complexity:O(d) time, O(1) extra space.
One line: peel digits from the right, grow the reverse on the left-to-right printout.
Frequently Asked Questions
A growing reverse-number pattern like 3, 32, 325, 3256, 32568 when starting from 86523.
Take the last digit with num % 10, append it with reverse = reverse * 10 + digit, then print reverse.
num = num / 10 removes the last digit so the loop can move to the next digit from the right.
Program 60 prints the shrinking original number. Program 61 builds and prints a growing partial reverse using modulo and multiplication.
Multiplying shifts existing digits left — reverse * 10 + digit appends the new digit on the right.
If the source ends in 0, that digit is extracted first — e.g. 120 gives 0, then 2, then 21.
You can print the partial reverse and the newline together with cout << reverse << "\n". That is the WriteLine-style end of each step.
O(d) where d is the number of digits — the loop runs once per digit.
🤔
Did you know?
Each iteration takes the last digit with num % 10, appends it to reverse via reverse = reverse * 10 + digit, prints the partial reverse, then shrinks num — runtime is O(d) for d digits.