Skip to content
C++ Better Explained
Go back
Reverse a Number in C++: Flip the Digits with a While Loop

Reverse a Number in C++

Reversing a number — turning 1234 into 4321 — is one of the most common beginner exercises, and for good reason. It teaches you the two-step pattern of pulling digits off a number one at a time, which shows up everywhere from digit sums to palindrome checks.


The Two Operations You Need

To reverse a number, you process it digit by digit using two operators:

For example, with 1234: 1234 % 10 is 4, and 1234 / 10 is 123. Do that repeatedly and you visit every digit from right to left.

#include <iostream>

int main() {
    int number = 1234;
    std::cout << "Last digit: " << number % 10 << "\n";  // 4
    std::cout << "Rest: " << number / 10 << "\n";         // 123
    return 0;
}

Building the Reversed Number

The reversal itself uses a while loop. Each pass, we take the last digit and append it to a reversed value. To “append,” we shift reversed one place to the left by multiplying by 10, then add the new digit:

#include <iostream>

int main() {
    int number = 1234;
    int reversed = 0;

    while (number != 0) {
        int digit = number % 10;      // grab last digit
        reversed = reversed * 10 + digit;  // shift left, add digit
        number = number / 10;         // drop last digit
    }

    std::cout << "Reversed: " << reversed << "\n";  // 4321
    return 0;
}

Trace it: reversed goes 0 -> 4 -> 43 -> 432 -> 4321, while number shrinks 1234 -> 123 -> 12 -> 1 -> 0. When number hits 0, the loop ends and reversed holds the answer.

Ready for the step after small programs? The jump from exercises like this to real projects is pointers, memory and OOP. C++ Better Explained covers all three in plain English, with analogies and code diagrams.

Get the book — $27

Handling Negative Numbers

If the user might enter a negative number like -123, the cleanest approach is to remember the sign, reverse the absolute value, then put the sign back:

#include <iostream>
#include <cstdlib>   // for std::abs

int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;

    int sign = (number < 0) ? -1 : 1;
    number = std::abs(number);

    int reversed = 0;
    while (number != 0) {
        reversed = reversed * 10 + number % 10;
        number /= 10;
    }

    std::cout << "Reversed: " << sign * reversed << "\n";
    return 0;
}

Reversing the absolute value keeps the loop simple, and reapplying sign at the end gives -123 -> -321.


A Note on Overflow

Reversing a large number can produce a value too big for an int. For example, reversing 1000000009 overflows a 32-bit int. For beginner exercises with small inputs this rarely bites, but if you want to be safe, use a wider type like long long for the reversed variable:

long long reversed = 0;

This is a good habit: whenever a calculation can grow past the input’s size, give the result room to breathe.


Where This Pattern Leads

The “peel a digit, shrink the number” loop is a building block. Swap the reversed = reversed * 10 + digit line for sum += digit and you get the sum of digits. Compare the reversed number to the original and you get a palindrome check. Master this one loop and a dozen classic problems open up.



Take Your C++ Further

C++ Better Explained ebook cover

Small programs like this one are the right way to practise. The next step is the part most beginners get stuck on: pointers, memory and OOP. C++ Better Explained covers all three in 87 plain-English pages, with analogies and code diagrams.

Get the book — $27

87 pages · plain English · 30-day money-back guarantee

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
Leap Year Program in C++: Check if a Year Is a Leap Year
Next Post
Star Pattern Programs in C++: Triangles, Pyramids, and Diamonds

Keep Learning