C++ Program to Reverse a Number



In this article, we will show you how to reverse a number using C++. Reversing a number means changing the order of its digits, so the first digit moves to the end, the last digit moves to the front, and the middle digits follow in the opposite order.

For example, if we start with the number 1234, the reversed number will be 4321.

In C++, we can reverse a number in different ways. Some common approaches we can use are:

Reversing a Number Using Loop

In this approach, we use a loop to get each digit from the number and build the reversed number. We keep taking the last digit, multiply the reversed number by 10, and add the digit.

Example

Here in this example, we use a while loop to get the last digit using the modulo operator(%), then multiply the reversed number by 10 and add the digit. We continue reducing the number by dividing it by 10, until all digits are processed.

#include <iostream>
using namespace std;

int reverseNumber(int num) {
    int reversed = 0;
    while (num != 0) {
        int digit = num % 10;  // Extract last digit
        reversed = reversed * 10 + digit;  // Add digit to the reversed number
        num /= 10;  // Remove last digit from the original number
    }
    return reversed;
}

int main() {
    int num = 1234; // Value directly assigned
    int reversed = reverseNumber(num);
    cout << "The reversed number of " << num << " is: " << reversed << endl;
    return 0;
}

The output from the program is displayed below, showing the reversed number:

The reversed number of 1234 is: 4321

Time Complexity: O(d) because it checks each digit one by one.

Space Complexity: O(1) because it only uses a few variables, no extra storage needed.

Reversing a Number Using Recursion

In this approach, we reverse the number using recursion. We break the problem into smaller parts by repeatedly calling a function that handles one digit at a time until all digits are reversed.

Example

In this example, we divide the number by 10 and pass the result as a parameter to the recursive function. We multiply the reversed result by 10 and add the last digit. The recursion stops when the number becomes zero.

#include <iostream>
using namespace std;

int reverseNumber(int num, int reversed = 0) {
    if (num == 0) return reversed;
    return reverseNumber(num / 10, reversed * 10 + num % 10);
}

int main() {
    int num = 1234; // Value directly assigned
    int reversed = reverseNumber(num);
    cout << "The reversed number of " << num << " is: " << reversed << endl;
    return 0;
}

The output below shows the reversed number:

The reversed number of 1234 is: 4321 

Time Complexity: O(n) because the function calls itself once per digit.

Space Complexity: O(n) each function calls uses memory.

Reversing a Number Using Stack

In this approach, we use a stack to store the digits and retrieve them in reverse order. The stack helps to reverse the digits easily.

Example

In this example, we push each digit of the number onto the stack and then pop them off one by one to form the reversed number. This method uses the Last-In-First-Out (LIFO) principle of stacks.

#include <iostream>
#include <stack>
using namespace std;

int reverseNumber(int num) {
    stack<int> digits;
    while (num != 0) {
        digits.push(num % 10);  // Push digit onto the stack
        num /= 10;  // Remove the last digit
    }

    int reversed = 0;
    while (!digits.empty()) {
        reversed = reversed * 10 + digits.top();  // Pop digit and form reversed number
        digits.pop();
    }
    return reversed;
}

int main() {
    int num = 1234; // Value directly assigned
    int reversed = reverseNumber(num);
    cout << "The reversed number of " << num << " is: " << reversed << endl;
    return 0;
}

Below is the output, which shows the reversed number:

The reversed number of 1234 is: 4321

Time Complexity: O(n), one loop to push digits and another to pop them, each running n times.

Space Complexity: O(n), stores all digits in a stack, so space depends on the number size.

Reversing a Number Using String Conversion

In this approach, we convert the number to a string, reverse the string, and then convert it back to an integer. This method is simple and relies on string manipulation.

Example

Here's a complete C++ program showing how we can reverse the number using string conversion.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int reverseNumber(int num) {
    string numStr = to_string(num);  // Convert number to string
    reverse(numStr.begin(), numStr.end());  // Reverse the string
    return stoi(numStr);  // Convert the reversed string back to integer
}

int main() {
    int num = 1234; // Value directly assigned
    int reversed = reverseNumber(num);
    cout << "The reversed number of " << num << " is: " << reversed << endl;
    return 0;
}

You will see the following output when you run the above program, which displays the reversed number.

The reversed number of 1234 is: 4321 

Time Complexity: O(d) because it changes the number to a string, reverses it, and changes it back.

Space Complexity: O(d) because the string and reversed string both require space based on the number of digits.

Updated on: 2025-05-09T15:18:28+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements