Reverse-Build Number Pattern in C++

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Modulo + Division

What You’ll Learn

How to print a number pattern in C++ by building the reverse of a number step-by-step and printing the partial reverse on each iteration.

This pattern is a practical way to practice digit extraction using % 10 and digit removal using / 10.

⭐ Pattern Output

For num = 86523, the output looks like this:

Output
3
32
325
3256
32568
1

Complete C++ Program

We repeatedly extract the last digit, append it to the reverse, print the partial reverse, and remove the last digit from the original number.

C++
#include <iostream>
using namespace std;

int main() {
    int num = 86523;
    int rev = 0;

    while (num != 0) {
        rev = rev * 10;
        rev = rev + (num % 10);
        cout << rev << "\n";
        num = num / 10;
    }

    return 0;
}

🧠 How It Works

1

Initialize variables

num holds the source number and rev starts at 0.

Setup
2

Extract last digit

num % 10 gives the last digit of the current number.

Digit
3

Append digit to the reverse

We shift rev left with rev * 10 and add the extracted digit.

Build
4

Remove last digit from the original

num /= 10 removes the last digit so the next iteration processes the next digit.

Trim
=

Growing reverse pattern

Each iteration adds one digit, so for a number with d digits, time complexity is O(d).

2

Variation — User Input Version

Read a number from the user and print the same reverse-build pattern.

C++
#include <iostream>
using namespace std;

int main() {
    long long num;
    long long rev = 0;

    cout << "Enter a number: ";
    cin >> num;

    if (num == 0) {
        cout << 0 << "\n";
        return 0;
    }

    if (num < 0) num = -num;

    while (num != 0) {
        rev = rev * 10 + (num % 10);
        cout << rev << "\n";
        num /= 10;
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Also print the extracted digit each step using digit = num % 10
  • Store each partial reverse in a vector and print later
  • Detect overflow for very large inputs (use long long carefully)
  • Try a string-based version to preserve trailing zeros
  • Combine with palindrome checks using the final reverse

Avoid

  • Using floating point operations for digit extraction
  • Forgetting to update num with num /= 10
  • Printing with endl inside the loop (unnecessary flush)
  • Ignoring the num == 0 case if you want 0 printed

Key Takeaways

1

num % 10 extracts the last digit.

2

rev = rev * 10 + digit appends a digit to the reverse.

3

The loop runs once per digit, so complexity is O(d).

4

This technique appears in problems like reverse numbers, digit sums, and palindromes.

❓ Frequently Asked Questions

Because we read digits from right-to-left (last digit first) and append them to rev. Each iteration adds one more digit to the end of rev.
Use a string approach (read input as a string) and build the reverse string step-by-step so zeros aren’t lost.
The loop won’t run, so handle it explicitly if you want to print 0 once.
O(d), where d is the number of digits.

Explore More C++ Number Patterns!

Patterns that use digit operations are a great bridge between math and programming.

All Number Patterns →
Did you know?

Building the reverse digit-by-digit is also used in checking whether a number is a palindrome (reads the same forwards and backwards).

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