Reverse a Number
Program to reverse the digits of a number
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 reverses the digits of a number. For example, if you enter 1234, it becomes 4321. This program demonstrates the while loop, modulo operator (%), integer division (/), and the concept of building a number digit by digit. Understanding number reversal is important for solving palindrome problems, number manipulation, and many algorithmic challenges.
---
1. What This Program Does
The program:
Examples:
1234
→ Output:
4321
567
→ Output:
765
100
→ Output:
1
(leading zeros are not displayed)
This is a fundamental problem that teaches:
---
2. Header File: #include <iostream>
#include <iostream>
Provides:
cout → for displaying outputcin → for reading input---
3. Declaring Variables
int num, reversed = 0, digit;
Variable `num`:
Variable `reversed`:
-
Initialized to 0
Variable `digit`:
numWhy initialize reversed to 0?
---
4. Taking Input From User
cin >> num;
numExample:
1234
num = 1234---
5. Saving Original Number
int original = num;
Why save the original?
num will be reduced to 0 during the reversal processIf we don't save it, we can't show the original number in the output.
---
6. Understanding the While Loop
while (num != 0)
How while loop works:
num not equal to 0?true
, execute the loop body
false
, exit the loop
Unlike for loop:
---
7. Extracting the Last Digit
digit = num % 10;
Understanding Modulo Operator (%)
The modulo operator gives the
remainder
after division.
`num % 10` extracts the last digit:
Examples:
1234 % 10 =4
(remainder when 1234 ÷ 10)
567 % 10 =7
(remainder when 567 ÷ 10)
89 % 10 =9
(remainder when 89 ÷ 10)
5 % 10 =5
(remainder when 5 ÷ 10)
Why this works:
4
→ last digit is 4
7
→ last digit is 7
---
8. Building the Reversed Number
reversed = reversed * 10 + digit;
This is the
key line
that builds the reversed number digit by digit.
How it works:
Step 1: Multiply by 10
Step 2: Add the digit
Step-by-step execution (reversing 1234):
Initial:
num = 1234, reversed = 0
Iteration 1:
digit = 1234 % 10 = 4reversed = 0 * 10 + 4 = 4num = 1234 / 10 = 123reversed = 4, num = 123Iteration 2:
digit = 123 % 10 = 3reversed = 4 * 10 + 3 = 43num = 123 / 10 = 12reversed = 43, num = 12Iteration 3:
digit = 12 % 10 = 2reversed = 43 * 10 + 2 = 432num = 12 / 10 = 1reversed = 432, num = 1Iteration 4:
digit = 1 % 10 = 1reversed = 432 * 10 + 1 = 4321num = 1 / 10 = 0reversed = 4321, num = 0Iteration 5:
num != 0 → 0 != 0 →false
-
Loop stops
Final result:
reversed = 4321 ✅
---
9. Removing the Last Digit
num = num / 10;
Integer Division (/):
Examples:
1234 / 10 =123
(not 123.4)
567 / 10 =56
(not 56.7)
89 / 10 =8
(not 8.9)
5 / 10 =0
(not 0.5)
Why this works:
---
10. Visual Representation
Reversing 1234:
Iteration 1:
Iteration 2:
Iteration 3:
Iteration 4:
Loop ends (num == 0)
---
11. Displaying the Result
This prints:
Output:
Reverse of 1234 is: 4321
---
12. Special Cases
Case 1: Single digit number
5
5
(same as input)
Case 2: Number ending with zeros
100
1
(leading zeros are not displayed)
Case 3: Palindrome number
1221
1221
(same as input - it's a palindrome!)
---
13. Why This Algorithm Works
The key insight:
Mathematical perspective:
We're essentially reading digits in reverse order and building the new number.
---
Summary
%) extracts the last digit/) removes the last digitreversed = reversed * 10 + digitThis program teaches essential number manipulation skills that are crucial for:
Understanding this algorithm helps you solve more complex problems involving number manipulation and digit processing.
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 "Loop 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.