Difficulty: Medium
Correct Answer: 345
Explanation:
Introduction / Context:
This routine uses recursion and a static accumulator to process the decimal digits of an integer. The base case threshold (Num / 100)
controls when to stop recursing, which affects how many trailing digits are accumulated and in what order during the unwind phase.
Given Data / Assumptions:
Dec = Num % 10
and then shrinks Num = Num / 10
.Num / 100
is nonzero (i.e., while Num >= 100).Sum
is static across calls and updated after potential recursion.
Concept / Approach:
The function pushes digits from right to left but only recurses while at least two digits remain (Num >= 100). Therefore, three least significant digits will be added during the unwind in the order encountered at return time, effectively yielding the last three digits of the original number in their original order (not fully reversed).
Step-by-Step Solution:
Verification / Alternative check:
If the base condition were if(Num)
, the entire number would be reversed to 54321. With the current condition, only the lowest three digits contribute to the final Sum.
Why Other Options Are Wrong:
Common Pitfalls:
Overlooking that Sum
is static (retained across calls) and that it is updated after the optional recursive call, which determines the order of accumulation.
Final Answer:
345
Discussion & Comments