Reverse a Number
How to Reverse a Number in C++ (6 Programs With Output)
C++ Reverse a Number Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int num, reversed = 0, remainder;
cout << "Enter a number: ";
cin >> num;
int original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
cout << "Reverse of " << original << " is: " << reversed << endl;
return 0;
}Enter a number: 1234 Reverse of 1234 is: 4321
Understanding Reverse a Number
This program teaches you how to reverse a number in C++. Reversing a number means reading its digits from right to left instead of left to right. For example, reversing 1234 gives 4321. This is a common programming problem that helps you understand digit manipulation, loops, and number operations.
---
1. What This Program Does
The program takes a number and reverses its digits. For example:
The process involves extracting digits from the right (one's place, ten's place, etc.) and building a new number from left to right.
Example:
---
2. Header File Used
This header provides:
---
#include <iostream>3. Declaring Variables
The program declares:
int num, reversed = 0, remainder;
---
4. Taking Input From the User
The program asks:
cin >> num;
We also save the original:
int original = num;
This is important because we'll modify num during the reversal process, and we need the original value for display.
---
cout << "Enter a number: ";5. The Reversal Algorithm (While Loop Method)
The core logic uses a while loop:
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
Let's break down how this works step-by-step for num = 1234:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
---
6. Understanding the Key Operations
Modulo Operator (%)
:
Integer Division (/)
:
Building the Reversed Number
:
---
7. Other Methods (Mentioned but not shown in code)
Method 2: Using For Loop
int reversed = 0;
for (int temp = num; temp != 0; temp /= 10) {
reversed = reversed * 10 + (temp % 10);
}
Method 3: Using Recursion
int reverseRecursive(int num, int rev = 0) {
if (num == 0) return rev;
}
return reverseRecursive(num / 10, rev * 10 + num % 10);Method 4: Using String Conversion
string str = to_string(num);
reverse(str.begin(), str.end());
int reversed = stoi(str);
Method 5: Using Stack
stack<int> digits;
while (num != 0) {
digits.push(num % 10);
num /= 10;
}
int reversed = 0, place = 1;
while (!digits.empty()) {
reversed = reversed * 10 + digits.top();
digits.pop();
}
Method 6: Using Array
int digits[10], count = 0;
while (num != 0) {
digits[count++] = num % 10;
num /= 10;
}
int reversed = 0;
for (int i = 0; i < count; i++) {
reversed = reversed * 10 + digits[i];
}
---
8. Displaying the Result
The program prints:
Output:
We use original (not num) because num was modified during the reversal process.
---
cout << "Reverse of " << original << " is: " << reversed << endl;9. Edge Cases and Special Numbers
Zero
:
Single Digit
:
Numbers Ending in Zero
:
Negative Numbers
:
Very Large Numbers
:
---
10. When to Use Each Method
-
While Loop
: Best for most cases - simple, efficient, easy to understand.
-
For Loop
: Alternative to while loop - same efficiency, different style.
-
Recursion
: Educational - helps understand recursive thinking.
-
String Conversion
: Simple but less efficient - good for learning string operations.
-
Stack
: Demonstrates stack usage - educational purpose.
-
Array
: Shows array manipulation - useful for learning arrays.
Best Practice
: Use the while loop method for most applications - it's efficient and straightforward.
---
11. Common Use Cases
Palindrome Checking
:
Number Manipulation
:
Algorithm Practice
:
---
12. Important Considerations
Integer Overflow
:
Leading Zeros
:
Negative Numbers
:
---
13. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning digit manipulation, understanding modulo and division operations, and preparing for more complex number-related algorithms in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Reverse a Number
This C++ program is part of the "Advanced Number Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.