Shape Rule
Growing reverse
Each line adds one digit on the right of the partial reverse — 3 → 32 → 325 → 3256 → 32568.

Program 61 prints a growing reverse-number pattern: starting from an integer like 86523, build the reverse one digit at a time and print each partial result — 3, 32, 325, and so on. This tutorial covers modulo, reverse building, a live preview, worked C++ examples, edge cases, and O(d) complexity.
Growing reverse
Each line adds one digit on the right of the partial reverse — 3 → 32 → 325 → 3256 → 32568.
num != 0
while (num != 0) repeats once per digit until the source number is fully consumed.
num % 10
num % 10 extracts the last digit — 3 from 86523, then 2 from 8652, and so on.
reverse * 10 + digit
reverse = reverse * 10 + (num % 10) shifts left and appends the new digit on the right.
Any integer
Enter a starting number and watch the growing reverse pattern in the browser.
Complexity
One iteration per digit — 86523 produces five lines; total steps equal digit count.
A growing reverse number pattern builds the reversed form of an integer one digit at a time and prints each partial result. With num = 86523, you get 3, 32, 325, 3256, 32568.
In C use while (num != 0), extract digits with num % 10, update reverse = reverse * 10 + digit, print, then num /= 10.
Modulo plus reverse building is the standard technique for reversing numbers, checking palindromes, and digit-sum problems.
num % 10 — last digit each step.
reverse * 10 + digit appends on the right.
Program 60 shrinks the original; Program 61 grows the partial reverse.
Follow Program 60; continue to Program 62 next.
In short: while (num != 0), reverse = reverse * 10 + num % 10, cout << reverse << "\n", then num /= 10.
Given starting integer num = 86523, build its reverse one digit at a time and print each partial reverse until all digits are processed.
// num = 86523
//3
//32
//325
//3256
//32568 | Item | Type | Description |
|---|---|---|
num | int | Source number — shrinks each iteration via / 10. |
reverse | int | Partial reverse — starts at 0, grows each step. |
| Extract digit | int | num % 10 — last digit of current num. |
| Append digit | int | reverse = reverse * 10 + digit. |
| Print step | void | cout << reverse << "\n" after each append. |
| Line count | int | Equals digit count of the starting number (86523 → 5 lines). |
reverse = 0
while num is not 0:
digit = num % 10
reverse = reverse * 10 + digit
print reverse
num = num / 10 | Approach | Idea | Best for |
|---|---|---|
| Two-step append | reverse *= 10; then reverse += digit | Teaching each operation separately |
| Combined append | reverse = reverse * 10 + (num % 10) | Compact production code |
| long long + llabs | Wider range and negative handling | User-input programs |
| Compact trace | num = 123 on paper first | Quick dry-runs |
long long | Wider integer type | Very large starting values |
| Goal | Pattern |
|---|---|
| Loop | while (num != 0) { ... } |
| Extract digit | int digit = num % 10; |
| Append to reverse | reverse = reverse * 10 + digit; |
| Print & shrink | cout << reverse << "\n"; num /= 10; |
Same growing-reverse pattern — three ways to set the starting number and trace the logic.
num = 86523Hard-coded start for demos
cin >> numRead starting number from console
num = 123Quick dry-run on paper
num % 10Last digit each step
rev * 10 + dAppend digit on the right
Reach for this pattern when teaching modulo, reverse building, and while loops together in C++.
Natural follow-up — same while loop, but build a growing reverse instead of shrinking the original.
Standard technique for reversing integers and checking palindromes.
Classic modulo + division question — explain extract-append-shrink before coding.
Compare single-number loops with 2D spiral grid filling.
Use long long when reversed values exceed int range.
Key benefit: one small program that locks in modulo, reverse building, and O(d) thinking.
Enter a positive integer between 10 and 99999999 and draw the growing reverse pattern in the browser.
Three complete C programs — fixed starting value, user input with long, and a compact num = 123 trace. Click View Output to reveal sample console results.
Print the growing reverse pattern for a hard-coded starting integer with a while loop.
num = 86523Hard-coded start — build reverse one digit at a time and print after each append.
#include <iostream>
using namespace std;
int main() {
int num = 86523;
int reverse = 0;
while (num != 0) {
reverse = reverse * 10;
reverse = reverse + (num % 10);
cout << reverse << "\n";
num = num / 10;
}
return 0;
} Extract 3 from 86523 → reverse becomes 3; then 2 → 32; then 5 → 325 — each line shows the partial reverse so far.
Read the starting number from the user with long long and handle negatives with llabs.
Read num with cin >> num (check cin.fail()), validate the result, and use combined append in one expression.
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
long long num;
cout << "Enter a number: ";
cin >> num;
if (cin.fail()) {
cout << "Please enter a valid integer.\n";
return 1;
}
num = llabs(num);
long long reverse = 0;
while (num != 0) {
reverse = reverse * 10 + (num % 10);
cout << reverse << "\n";
num /= 10;
}
return 0;
} Same loop core as Example 1; uses long long for wider range and combines multiply-and-add into one line.
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.
#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;
} Three iterations: extract 3 → reverse 3; extract 2 → reverse 32; extract 1 → reverse 321.
num is the source number; reverse starts at 0.
num % 10 gives the current last digit (3, then 2, then 5, …).
reverse = reverse * 10 + digit shifts existing digits left and appends the new one.
cout << reverse << "\n" then num /= 10 moves to the next digit.
One line per digit processed — O(d) time for d digits, O(1) extra memory.
num = 86523Trace each loop iteration: extract the digit, update reverse, print, then shrink num.
| Step | num | Digit num % 10 | reverse after append | Printed |
|---|---|---|---|---|
| 1 | 86523 | 3 | 3 | 3 |
| 2 | 8652 | 2 | 32 | 32 |
| 3 | 865 | 5 | 325 | 325 |
| 4 | 86 | 6 | 3256 | 3256 |
| 5 | 8 | 8 | 32568 | 32568 |
Total lines printed: 5 = digit count of the starting number. Final reverse equals the full reversed number.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
num % 10 extracts digits — concrete introduction to the remainder operator.
Example: trace num = 123 and watch reverse grow 3, 32, 321.
Standard building block for reversing integers and palindrome checks.
Example: stop after the loop and compare final reverse to the original.
Program 60 shrinks the original; Program 61 grows the partial reverse — same loop, different output.
Example: print both patterns side by side for 86523.
Move from single-number loops to 2D spiral grid filling next.
Example: compare while-loop digit work with nested boundary loops.
One iteration per digit — makes O(d) concrete for beginners.
Example: num = 1000000 produces seven lines — seven digits.
Pair the pattern with cin >> num and overflow checks.
Example: use long when reversed values may exceed int range.
Pro Tip: when an interviewer asks for reverse building, explain extract-append-shrink before writing the loop.
Why this pattern earns a permanent spot in beginner C courses.
Wrong append order or missing shrink shows up immediately as broken output.
Modulo and integer division together — essential digit-manipulation toolkit.
Print both num and reverse, collect steps in a list, or check palindromes with small edits.
Only num and reverse needed — no arrays required.
Pro Tip: trace num and reverse on paper for 123 before coding — watch reverse grow 3, 32, 321.
Small habits that keep growing-reverse code clean.
reverse = 0 before the loop — first digit becomes the first printed value.
Use long long num with cin >> num when values may be large.
reverse * 10 + digit — multiply shifts left, then append the new digit.
num /= 10 after printing — without it the loop never advances.
Trace num = 123 on paper before coding larger demos.
Pro Tip: if reverse stays at single digits, you probably forgot to multiply by 10 before adding.
Mistakes that commonly break growing-reverse patterns.
Adding digits without shifting leaves reverse as single digits — 3, 2, 5 instead of 3, 32, 325.
→ Always do reverse = reverse * 10 + digit.
Without num /= 10, the same digit is extracted forever — infinite loop.
→ Divide by 10 after each append and print.
Large inputs can overflow int when building reverse — silent wrong results.
→ Use long long for large numbers.
Letters or empty input leave num unread when cin is unchecked.
→ Check cin.fail() after cin >> num and re-prompt on failure.
Float division on digits introduces precision errors — stick to integer operations.
→ Keep num and reverse as integer types.
Check these inputs before calling the solution done.
while (num != 0) never runs — print nothing or show a message.
One line prints 7 — simplest case.
Builds 0, 2, 21 — zero is extracted first from the right.
Apply llabs before the loop — see Example 2.
Unchecked cin leaves num uninitialized — check the return value.
Use long long when reverse exceeds int range.
Try these variations to lock in the pattern.
num = 123 before codingreverse * 10 shifts existing digits left before the new digit is appended on the right.long when reversed values may exceed int.MaxValue.reverse after the loop equals the fully reversed number — 32568 for input 86523.Quick Takeaway: while (num != 0), reverse = reverse * 10 + num % 10, print, then num /= 10.
| 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 growing reverse number pattern is a small while-loop exercise with lasting payoff: modulo, reverse building, and O(d) intuition. Master the fixed-value version, then try user input with long and the compact num = 123 trace.
Practice the three examples above, then continue to Program 62 for the next pattern in the series.
Extract with modulo, append with multiply-and-add, print reverse, then shrink num — validate input when reading from the console.
reverse = 0 before the loopreverse = reverse * 10 + (num % 10)cin >> num with cin.fail() checks for large user inputnum /= 10 inside the loopnum = 123 dry-run before larger demosPrint the growing reverse pattern the beginner-friendly way.
Build reverse digit by digit
Definitionnum % 10 extracts digit
Mathreverse * 10 + digit
Codenum /= 10 each step
LoopO(d) time
AnalysisEach 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.
Move on to the next pattern in the C++ number-pattern series.
12 people found this page helpful