Problem 5 • Math
Author: Er. Manoj Kumar • Computer Science Fundamentals
Reverse an Integer and Check for Palindrome
📋
Problem Statement
Given an integer N, extract its digits from right to left using modulo arithmetic to construct its reverse number, and verify whether the original number equals its reverse (Palindrome check).
📊
Variable Dictionary / Table
| Variable Name | Data Type | Role & Purpose |
|---|---|---|
| N | Long Long | Input integer |
| orig | Long Long | Preserves initial value for comparison |
| rev | Long Long | Accumulates reversed digits (rev * 10 + rem) |
| rem | Integer | Last digit extracted (temp % 10) |
📝
Step-by-Step Formal Algorithm
Step 1: Start.
Step 2: Read integer N.
Step 3: Set orig = N, rev = 0.
Step 4: If N < 0, set temp = -N; Else temp = N.
Step 5: While (temp > 0):
rem = temp % 10
rev = (rev * 10) + rem
temp = temp / 10
Step 6: Print 'Reversed Number:', rev.
Step 7: If (N >= 0 and orig == rev):
Print 'It is a Palindrome Number.'
Step 8: Else:
Print 'It is NOT a Palindrome Number.'
Step 9: Stop.
🔀
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/"Read N"/]
In --> Set["orig = N
rev = 0
temp = abs(N)"] Set --> Cond{"Is temp > 0 ?"} Cond -- Yes --> Calc["rem = temp % 10
rev = rev * 10 + rem
temp = temp / 10"] Calc --> Cond Cond -- No --> PrintRev[/"Print rev"/] PrintRev --> Chk{"Is N >= 0 and orig == rev ?"} Chk -- Yes --> OutP[/"Print 'Palindrome Number'"/] OutP --> Stop([Stop]) Chk -- No --> OutNP[/"Print 'Not Palindrome'"/] OutNP --> Stop
rev = 0
temp = abs(N)"] Set --> Cond{"Is temp > 0 ?"} Cond -- Yes --> Calc["rem = temp % 10
rev = rev * 10 + rem
temp = temp / 10"] Calc --> Cond Cond -- No --> PrintRev[/"Print rev"/] PrintRev --> Chk{"Is N >= 0 and orig == rev ?"} Chk -- Yes --> OutP[/"Print 'Palindrome Number'"/] OutP --> Stop([Stop]) Chk -- No --> OutNP[/"Print 'Not Palindrome'"/] OutNP --> Stop
🖥️
Flowchart (ANSI Terminal ASCII Chart)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read N |
+-----------------+
|
v
+-----------------+
| orig = N, rev = 0|
| temp = abs(N) |
+-----------------+
|
v
/ \
/ temp > 0?\ ---NO---> [Print rev]
\ / |
| YES v
v / \
+-----------------+ /orig==rev?\
| rem = temp % 10 | \ /
| rev = rev*10+rem| YES/ \NO
| temp = temp / 10| v v
+-----------------+ [Palin] [Not Palin]
| | |
+------> +----+---+
|
v
[STOP]
🧪
Dry-Run Execution Trace Table
| Execution Step / Case | Variables State 1 | Variables State 2 | Condition Evaluation | Output / Result |
|---|---|---|---|---|
| N = 12321 | orig=12321 | Digits: 1, 2, 3, 2, 1 | rev = 12321 == orig | PALINDROME |
| N = 456 | orig=456 | Digits: 6, 5, 4 | rev = 654 != 456 | NOT PALINDROME |
⚡
Interactive C++ Compiler & Runner
Live In-BrowserEdit the code directly with the Edit Code button, provide custom inputs, and press Run Code to compile and execute in the live terminal console below!
📄
problem_5.cpp
GCC 13.2 • C++20
#include <iostream>
using namespace std;
int main() {
long long n;
cout << "Enter an integer: ";
if (!(cin >> n)) {
cout << "Invalid input! Please enter an integer.\n";
return 1;
}
long long original = n;
long long temp = (n < 0) ? -n : n;
long long reversed = 0;
while (temp > 0) {
long long rem = temp % 10;
reversed = (reversed * 10) + rem;
temp /= 10;
}
cout << "\nOriginal Value : " << original << "\n";
cout << "Reversed Value : " << reversed << "\n";
if (n >= 0 && original == reversed) {
cout << "Result : " << original << " is a PALINDROME number.\n";
} else {
cout << "Result : " << original << " is NOT a palindrome number.\n";
}
return 0;
}
⌨️ Standard Input (cin):
Presets:
💻 Terminal Output
• Ready to run
Input: 12321 Output: Original Value : 12321 Reversed Value : 12321 Result : 12321 is a PALINDROME number.
Developer & Exam Key Takeaway
Mathematical digit extraction rev = rev * 10 + rem uses O(log10(N)) iterations and zero string allocations.