Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Practical 6: Palindrome Number Check

Lesson 7 of 14 in the free C# Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Aim

To check whether an integer reads the same forwards and backwards by reversing it arithmetically — with no string conversion and no heap allocation.

Theory

The arithmetic reversal rev = rev * 10 + (temp % 10) is a positional-notation rebuild: each step shifts the accumulated value one decimal place left and appends the newly extracted digit. When every digit has been consumed, rev holds the mirror image and palindromicity reduces to a single integer comparison rev == num. The algorithm is O(d) time, O(1) space and completely allocation-free — contrast with the string route (new string(num.ToString().Reverse().ToArray())), which allocates several heap objects per check and is measurably slower under load. The advanced concern is overflow: reversing a perfectly valid int such as 1463847412 produces a value beyond int.MaxValue (2147483647). In C#'s default unchecked context the multiplication silently wraps around modulo 2³²; wrapping the loop in a checked { } block would raise OverflowException instead — deterministic failure always beats silent corruption (this is the core trap of LeetCode problem 7). Finally, the loop condition temp > 0 means negative inputs never execute the body, so they are classified as non-palindromes by convention.

Requirements

  • .NET SDK 8.0+ or the RUN CODE button; commands: dotnet new console, dotnet run.

Procedure

  1. Create the project: dotnet new console -n PalinNum, cd PalinNum, paste the code into Program.cs.
  2. Run dotnet run with the default 1221.
  3. Re-test with 12321 (odd length, palindrome) and 1234 (not a palindrome) so both branches appear in the record book.
  4. Optional: wrap the loop body in checked { } and feed a 10-digit-reversal case to observe OverflowException.

Explanation of the Code

  • int temp = num, rev = 0; — the working copy protects the original; rev is the accumulator seeded with 0.
  • Each iteration of while (temp > 0): temp % 10 extracts the units digit, rev * 10 makes room for it, and the sum appends it. Trace for 1221: rev = 1 → 12 → 122 → 1221.
  • temp /= 10; uses truncating integer division to discard the processed digit; when temp reaches 0 every digit has been consumed and the loop exits.
  • Console.WriteLine(rev == num ? ... : ...) prints the verdict via the ternary operator and string interpolation — num is still pristine because only temp was mutated.

Expected Output

1221 is Palindrome.

With num = 12341234 is not Palindrome.; with num = 1232112321 is Palindrome.

🎯 Viva Questions

  1. Why is the arithmetic method preferred over string reversal? — No heap allocations, O(1) space, faster; strings are immutable so reversal creates new objects.
  2. Trace rev for input 121. — 1, then 12, then 121.
  3. What happens on overflow while reversing? — The default unchecked context wraps silently; a checked block raises OverflowException.
  4. Is -121 a palindrome here? — No; the loop never runs for non-positive values, leaving rev = 0, which differs from -121.
  5. State the loop invariant of the reversal. — After k iterations, rev equals the last k digits of the original number, reversed.
  6. Complexity of the check? — O(d) time for d digits and O(1) extra space.