C++ recursive base-13 conversion using a class method: determine the output when the initial value is 130.
#include
class Number
{
int Num;
public:
Number(int x = 0) { Num = x; }
void Display() { cout << Num; }
void Modify();
};
void Number::Modify()
{
int Dec;
Dec = Num % 13;
Num = Num / 13;
if (Num > 0) Modify();
if (Dec == 10) cout << "A";
else if (Dec == 11) cout << "B";
else if (Dec == 12) cout << "C";
else if (Dec == 13) cout << "D"; // unreachable for base-13 remainders
else cout << Dec;
}
int main()
{
Number objNum(130);
objNum.Modify();
return 0;
}
-
A130
-
BA0
-
CB0
-
D90
-
ENone of the above
Answer
Correct Answer: A0
Explanation
Introduction / Context: This program prints a number in base 13 using digits 0–9 and letters A, B, C for 10, 11, 12 respectively. The method Modify transforms Num by repeatedly dividing by 13, recursing for higher places, and then printing the current remainder as a base-13 digit.
Given Data / Assumptions:
- Initial Num = 130.
- Remainder mapping: 10 → A, 11 → B, 12 → C.
- Recursion prints higher-order digits first (after dividing), then the current remainder.
Concept / Approach: Converting to base 13: at each step compute Dec = Num % 13 and Num = Num / 13. Recurse if Num > 0 to print the more significant digits first; then print the symbol for Dec. This is the standard recursive number-to-base printing pattern.
Step-by-Step Solution:
Start: Num = 130 → Dec = 130 % 13 = 0; Num = 130 / 13 = 10; recurse.Second frame: Num = 10 → Dec = 10 % 13 = 10; Num = 10 / 13 = 0; no recursion.Map Dec = 10 → print "A".Return to first frame → print Dec = 0 as "0".Concatenate outputs → "A0".Verification / Alternative check: 130 in base 13 equals 10*13 + 0, so the digits are 10 and 0 → A0. Printing order matches the recursion.
Why Other Options Are Wrong:
- 130: That would be base 10, not base 13 representation.
- B0 or 90: Incorrect remainder mapping or arithmetic.
- None of the above: A0 is exactly produced.
Common Pitfalls: Expecting 13 to produce a remainder of 13 (remainders are 0–12), or forgetting that the routine prints higher-order digits first by recursing before output.
Final Answer: A0