C++ Number Pattern (Progressive Reverse)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

Remember
Rule: reverse = reverse * 10 + (num % 10); print; num /= 10

3
32
325
3256
32568     ← start = 86523

Unlike Program 60 (print then shrink the original), this pattern extracts digits from the right and grows a new reverse value.

How to Solve It

Loop while the source is not zero: extract the last digit, append it to reverse, print, then shrink the source.

MethodIdeaBest for
While + moduloExtract, append, print, shrinkLearning, interviews, demos
Compact traceSame logic with num = 123Quick 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

GoalPattern
Loop until emptywhile (num != 0)
Extract last digitint digit = num % 10;
Append to reversereverse = reverse * 10 + digit;
Print + shrinkcout << reverse << "\n"; num /= 10;
Initializeint reverse = 0; before the loop

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << reverseStays on the same lineThe partial reverse
cout << "\n" / cout << reverse << "\n"Ends the lineEach growing step

Each step prints one complete partial reverse and ends the line (WriteLine-style).

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 result 86523 · 5 lines
3
32
325
3256
32568

Worked Walkthrough

Trace num = 86523 — extract, append, print, then shrink.

Stepnum % 10reverse after appendAfter num /= 10
1338652
2232865
3532586
4632568
58325680 (loop ends)

Five lines for a five-digit start — the final reverse equals the fully reversed number.

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;
}

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;
}

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;
}

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.

Time and Space Complexity

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

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.

Next: Perfect Square Spiral

Continue with the next pattern in the C++ number-pattern series.

Program 62 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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