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 8: Reverse a Number

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

Aim

To reverse the decimal digits of an integer using modulo/division arithmetic, mastering the positional-notation mechanics that underlie palindrome checks, digit sums and base conversion.

Theory

Digit reversal is a fold over the decimal representation: repeatedly peel the least significant digit with temp % 10 and push it into an accumulator that is scaled by ten each round (rev = rev 10 + digit). The result is O(d) time, O(1) space, and zero heap allocation. Three subtleties elevate this from a toy to an engineering exercise. (1) Trailing zeros vanish: 1200 reverses to 21, because an int has no way to represent leading zeros — positional notation assigns them no value. (2) Overflow: reversing 1999999999 (a valid int) yields 9999999991, far beyond int.MaxValue; in the default unchecked context the value silently wraps, while a long accumulator or a checked block converts silent corruption into a detectable condition — this is the entire difficulty of LeetCode problem 7. (3) Negative input: the guard temp > 0 skips negatives entirely; supporting them means capturing the sign, reversing Math.Abs(num), then re-applying the sign. At the machine level, the JIT strength-reduces the 10 into shift-and-add instructions (x·10 = (x << 3) + (x << 1)), so each iteration is a handful of cycles.

Requirements

  • .NET SDK 8.0+ with the dotnet CLI, or the built-in RUN CODE button.

Procedure

  1. Create the project: dotnet new console -n ReverseNum, cd ReverseNum, paste the code.
  2. Run dotnet run with the default 12345.
  3. Re-run with 1200 to observe the reversal truncating to 21.
  4. Optional: change the accumulator to long and reverse 1999999999 to see the true value that int would have corrupted.

Explanation of the Code

  • int temp = num, rev = 0; — non-destructive design: num is preserved so both the original and the reversed value can be reported at the end.
  • Loop trace for 12345: rev = 5 → 54 → 543 → 5432 → 54321 while temp shrinks 1234 → 123 → 12 → 1 → 0. The invariant: after k steps, rev holds the last k digits of the input, reversed.
  • temp /= 10; truncates toward zero (C# integer division), so a positive temp strictly decreases — guaranteeing termination in exactly d iterations.
  • The two interpolated Console.WriteLine calls report before/after — note {num} still prints 12345, proving the working copy protected the original.

Expected Output

Original: 12345
Reversed: 54321

With num = 1200Reversed: 21 (trailing zeros are lost by design).

🎯 Viva Questions

  1. What happens to trailing zeros? — They disappear: 1200 → 21; an int cannot encode leading zeros.
  2. How would you make reversal overflow-safe? — Accumulate into a long, use checked, or pre-check rev against (int.MaxValue - digit) / 10 before scaling.
  3. How many iterations for an n-digit number? — Exactly n, i.e. O(log₁₀ num).
  4. Why is the loop guaranteed to terminate? — Integer division by 10 strictly decreases any positive temp until it reaches 0.
  5. How would you support negative numbers? — Reverse Math.Abs(num) and re-apply the sign afterwards.
  6. **What optimisation does the JIT apply to 10?* — Strength reduction into shift/add instructions, e.g. (x << 3) + (x << 1).