Solution Converting Decimal to Binary
Problem Statement
Write a Recursive Procedure to Convert a Decimal Number to a Binary Equivalent.
Given a decimal number, we need to convert it to its binary representation, as explained in the following table:
| Input(s) | Output(s) | Explanation |
|---|---|---|
| Decimal Number = 10 | Binary Number = 1010 | The decimal number 10 can be represented as 1010 in binary. Each digit in the binary representation corresponds to the powers of 2. |
| Decimal Number = 27 | Binary Number = 11011 | The decimal number 27 can be represented as 11011 in binary. Each digit in the binary representation corresponds to the powers of 2. |
| Decimal Number = 5 | Binary Number = 101 | The decimal number 5 can be represented as 101 in binary. Each digit in the binary representation corresponds to the powers of 2. |
Constraints:
- 0 <= n <= 109
Solution
The algorithm to convert a decimal number to a binary number using recursion can be defined as follows:
Step-by-Step Algorithm
-
Method
decimalToBinary(int decimal):- Check if
decimalis0.- Return
"0"iftruebecause the binary representation of0is"0".
- Return
- Call the helper method
decimalToBinaryRec(decimal / 2)to start building the binary string. - Append
(decimal % 2)to the result of the recursive call to capture the current bit.
- Check if
-
Recursive Method
decimalToBinaryRec(int decimal):- Base Case:
- If
decimalis0, return"". This stops the recursion because there are no more bits to process.
- If
- Recursive Call:
- Call
decimalToBinaryRec(decimal / 2)to process the next bit to the left by dividingdecimalby2.
- Call
- Append Current Bit:
- Append
(decimal % 2)to the result of the recursive call. This captures the current bit, either0or1.
- Append
- Base Case:
-
Construct the Result:
- The binary string is built as recursion unwinds, with each call contributing one bit to the final result.
-
Return Final Output:
- The constructed binary string is returned from the initial
decimalToBinarycall. This represents the complete binary representation of the input decimal.
- The constructed binary string is returned from the initial
Algorithm Walkthrough
Input: decimal = 10
-
Initial Call:
decimalToBinary(10)- Not
0, so calldecimalToBinaryRec(10 / 2)→decimalToBinaryRec(5). - Append
(10 % 2)=0at the end.
- Not
-
Recursive Call 1 (
decimal = 5):decimalToBinaryRec(5)- Not
0, so calldecimalToBinaryRec(5 / 2)→decimalToBinaryRec(2). - Append
(5 % 2)=1at the end.
- Not
-
Recursive Call 2 (
decimal = 2):decimalToBinaryRec(2)- Not
0, so calldecimalToBinaryRec(2 / 2)→decimalToBinaryRec(1). - Append
(2 % 2)=0at the end.
- Not
-
Recursive Call 3 (
decimal = 1):decimalToBinaryRec(1)- Not
0, so calldecimalToBinaryRec(1 / 2)→decimalToBinaryRec(0). - Append
(1 % 2)=1at the end.
- Not
-
Base Case (
decimal = 0):decimalToBinaryRec(0)returns"".
-
Unwinding Recursion:
- Return from Recursive Call 3:
"" + "1"→"1". - Return from Recursive Call 2:
"1" + "0"→"10". - Return from Recursive Call 1:
"10" + "1"→"101". - Return from Initial Call:
"101" + "0"→"1010".
- Return from Recursive Call 3:
Final Output:
- The binary representation of
10is"1010".
Code
Here is the code for this algorithm:
public class Solution {
public String decimalToBinary(int decimal) {
if (decimal == 0) {
return "0";
}
return decimalToBinaryRec(decimal / 2) + (decimal % 2); // Recursive call
}
private String decimalToBinaryRec(int decimal) {
if (decimal == 0) {
return ""; // Base case
}
return decimalToBinaryRec(decimal / 2) + (decimal % 2); // Recursive call
}
public static void main(String[] args) {
Solution sol = new Solution();
int[] examples = { 10, 27, 5 };
for (int example : examples) {
String binary = sol.decimalToBinary(example);
System.out.println(
"Binary representation of " + example + " is: " + binary
);
}
}
}
Time and Space Complexity
The time complexity of the algorithm is
The space complexity is
🤖 Don't fully get this? Learn it with Claude
Stuck on Solution Converting Decimal to Binary? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Solution Converting Decimal to Binary** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Solution Converting Decimal to Binary** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Solution Converting Decimal to Binary**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Solution Converting Decimal to Binary**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.