Decimal to Binary
Java coding interview problem for Basic Number Programs: Decimal to Binary.
Converting a Decimal Number to its Binary Representation is one of the most common Java coding interview questions. It helps interviewers evaluate your understanding of number systems, loops, arithmetic operations, and problem-solving skills.
Binary conversion is an important concept in computer science because computers internally store and process data in binary format.
What is a Decimal Number?
A Decimal Number uses the Base-10 number system.
It contains digits from
0 to 9
Examples
10
25
100
255
1024
What is a Binary Number?
A Binary Number uses the Base-2 number system.
It contains only
0
1
Examples
1010
11001
11111111
10000000000
Every decimal number can be represented in binary.
Decimal to Binary Conversion Table
| Decimal | Binary |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 10 |
| 3 | 11 |
| 4 | 100 |
| 5 | 101 |
| 6 | 110 |
| 7 | 111 |
| 8 | 1000 |
| 9 | 1001 |
| 10 | 1010 |
| 15 | 1111 |
| 16 | 10000 |
Real Interview Question
Write a Java program to convert a decimal number into its binary representation without using any built-in methods.
Understanding the Logic
The idea is simple.
Repeatedly divide the decimal number by 2.
At every step,
store the remainder.
Continue until the number becomes zero.
Finally,
read all remainders in reverse order.
Visual Representation
Input
13
Division Process
13 ÷ 2 = 6
Remainder = 1
↓
6 ÷ 2 = 3
Remainder = 0
↓
3 ÷ 2 = 1
Remainder = 1
↓
1 ÷ 2 = 0
Remainder = 1
Collected Remainders
1
0
1
1
Read from bottom to top
1101
Therefore
13(decimal)
=
1101(binary)
Why Read in Reverse?
The first remainder becomes the least significant bit (LSB).
The last remainder becomes the most significant bit (MSB).
Example
13
Collected
1
0
1
1
Reverse Order
1101
Brute Force Approach
The simplest solution is
- Divide the number by 2
- Store the remainder
- Continue until the number becomes 0
- Print the remainders in reverse order
Algorithm
Step 1
Read the decimal number.
Step 2
Handle the special case.
If number == 0
Output = 0
Step 3
Initialize an empty binary number.
Step 4
Repeat while
number > 0
Step 5
Find the remainder.
remainder = number % 2;
Step 6
Store the remainder.
Step 7
Divide the number.
number = number / 2;
Step 8
Repeat until the number becomes zero.
Step 9
Reverse the stored digits.
Step 10
Print the binary number.
Dry Run
Input
10
| Iteration | Number | Remainder | Stored |
|---|---|---|---|
| 1 | 10 | 0 | 0 |
| 2 | 5 | 1 | 01 |
| 3 | 2 | 0 | 010 |
| 4 | 1 | 1 | 0101 |
Reverse
1010
Output
1010
Another Dry Run
Input
25
| Iteration | Number | Remainder | Stored |
|---|---|---|---|
| 1 | 25 | 1 | 1 |
| 2 | 12 | 0 | 10 |
| 3 | 6 | 0 | 100 |
| 4 | 3 | 1 | 1001 |
| 5 | 1 | 1 | 10011 |
Reverse
11001
Output
11001
Approach 1 — Using Repeated Division
This is the most common interview solution.
Complete Java Program
public class DecimalToBinary {
public static void main(String[] args) {
int number = 25;
int[] binary = new int[32];
int index = 0;
if (number == 0) {
System.out.println("Binary = 0");
return;
}
while (number > 0) {
binary[index] = number % 2;
index++;
number = number / 2;
}
System.out.print("Binary = ");
for (int i = index - 1; i >= 0; i--) {
System.out.print(binary[i]);
}
}
}
Output
Binary = 11001
Step-by-Step Code Explanation
Step 1
Declare the decimal number.
int number = 25;
Current value
25
Step 2
Create an array.
int[] binary = new int[32];
The array stores the binary digits (remainders).
Step 3
Initialize the index.
int index = 0;
Initially
index = 0
Step 4
Handle zero.
if (number == 0)
Output
Binary = 0
Step 5
Find the remainder.
number % 2
Example
25 % 2 = 1
Store
1
Step 6
Divide by 2.
number = number / 2;
Example
25
↓
12
↓
6
↓
3
↓
1
↓
0
Step 7
Repeat
Continue until
number == 0
Each iteration produces one binary digit.
Step 8
Print in reverse.
for (int i = index - 1; i >= 0; i--)
The digits are printed from the last stored remainder to the first.
Example
Stored
1
0
0
1
1
Printed
11001
Why Does This Work?
Each division by 2 extracts one binary digit.
The remainder can only be
0
or
1
These remainders represent the binary digits from least significant bit (LSB) to most significant bit (MSB).
Printing them in reverse order produces the correct binary representation.
Advantages of This Approach
- Easy to understand.
- Uses only arithmetic operators.
- Does not rely on built-in Java methods.
- Demonstrates understanding of number systems.
- Frequently asked in Java coding interviews.
- Efficient for integer conversion.
Drawbacks
Although this solution is the most common interview approach, interviewers often ask follow-up questions such as:
- Can you solve it using a Stack?
- Can you use
Integer.toBinaryString()? - Can you create a reusable method?
- How would you convert negative numbers?
- Which approach is the most efficient?
In the next part, we'll cover:
- Stack-based solution
- Built-in Java method (
Integer.toBinaryString()) - Reusable method approach
- Time and space complexity
- Comparison of all approaches
- Common interview mistakes
- Frequently asked interview questions
- Related coding problems
- Key takeaways
- Interview tips
Approach 2 — Using Stack
A Stack follows the Last In, First Out (LIFO) principle.
Since binary remainders are generated from Least Significant Bit (LSB) to Most Significant Bit (MSB), a Stack automatically prints them in the correct order.
Logic
Input
13
Generated Remainders
1
0
1
1
Push into Stack
Top
1
1
0
1
Pop Elements
1101
Java Program
import java.util.Stack;
public class DecimalToBinaryStack {
public static void main(String[] args) {
int number = 25;
Stack<Integer> stack = new Stack<>();
if (number == 0) {
System.out.println("Binary = 0");
return;
}
while (number > 0) {
stack.push(number % 2);
number = number / 2;
}
System.out.print("Binary = ");
while (!stack.isEmpty()) {
System.out.print(stack.pop());
}
}
}
Output
Binary = 11001
Approach 3 — Using Integer.toBinaryString()
Java provides a built-in method for converting decimal numbers to binary.
This is the easiest approach but is usually not preferred in coding interviews because interviewers often want to evaluate your understanding of the conversion algorithm.
Java Program
public class DecimalToBinaryBuiltIn {
public static void main(String[] args) {
int number = 25;
String binary = Integer.toBinaryString(number);
System.out.println("Binary = " + binary);
}
}
Output
Binary = 11001
Approach 4 — Using a Reusable Method
Creating a reusable method improves
- Code reusability
- Readability
- Maintainability
- Unit testing
Java Program
public class DecimalToBinaryMethod {
static String convertToBinary(int number) {
if (number == 0) {
return "0";
}
StringBuilder binary = new StringBuilder();
while (number > 0) {
binary.append(number % 2);
number = number / 2;
}
return binary.reverse().toString();
}
public static void main(String[] args) {
int number = 45;
System.out.println("Binary = " + convertToBinary(number));
}
}
Output
Binary = 101101
Time Complexity
Array-Based Solution
| Operation | Complexity |
|---|---|
| Time | O(log₂ n) |
| Space | O(log₂ n) |
Stack Solution
| Operation | Complexity |
|---|---|
| Time | O(log₂ n) |
| Space | O(log₂ n) |
Built-in Method
| Operation | Complexity |
|---|---|
| Time | O(log₂ n) |
| Space | O(log₂ n) |
Comparison of All Approaches
| Approach | Time | Space | Recommended |
|---|---|---|---|
| Repeated Division + Array | O(log n) | O(log n) | ✅ Best for Interviews |
| Stack | O(log n) | O(log n) | Easy to Understand |
| Reusable Method | O(log n) | O(log n) | Production Ready |
| Integer.toBinaryString() | O(log n) | O(log n) | Quick but Less Preferred |
Common Mistakes
Mistake 1
Printing the remainders immediately.
Wrong
while (number > 0) {
System.out.print(number % 2);
number = number / 2;
}
Output for
13
becomes
1011
Correct Output
1101
Always print the remainders in reverse order.
Mistake 2
Ignoring the special case
0
Expected Output
0
Always check
if (number == 0)
before starting the loop.
Mistake 3
Using floating-point division.
Wrong
number = number / 2.0;
Correct
number = number / 2;
Use integer division.
Mistake 4
Using a fixed-size array without tracking the index.
Always maintain an index to know how many binary digits have been stored.
Mistake 5
Using the built-in method when the interviewer asks
"Convert the number without using built-in methods."
In that case,
implement the repeated division algorithm manually.
Interview Follow-up Questions
Q1. Convert Binary to Decimal.
Q2. Convert Decimal to Octal.
Q3. Convert Decimal to Hexadecimal.
Q4. Convert Binary to Octal.
Q5. Convert Binary to Hexadecimal.
Q6. Count the number of 1s in the binary representation.
Q7. Determine whether a number is a power of 2.
Q8. Explain why the binary digits are printed in reverse order.
Q9. Convert a negative decimal number to binary.
Q10. Compare manual conversion with Integer.toBinaryString().
Related Coding Problems
- Binary to Decimal
- Decimal to Octal
- Decimal to Hexadecimal
- Reverse Integer
- Count Digits
- Check Even or Odd
- Count Set Bits
- Power of Two
Key Takeaways
- Binary uses only 0 and 1.
- Decimal-to-binary conversion is performed by repeatedly dividing the number by 2.
- Each remainder represents one binary digit.
- The remainders are generated from Least Significant Bit (LSB) to Most Significant Bit (MSB), so they must be printed in reverse order.
- The repeated division algorithm is the most common interview solution.
Integer.toBinaryString()is convenient but is usually not accepted when interviewers expect the manual conversion algorithm.- All approaches run in O(log n) time because the number is divided by 2 in every iteration.
Interview Tip
If an interviewer asks:
"Write a Java program to convert a decimal number to binary."
Start with the repeated division by 2 approach. Explain that each division produces one binary digit as a remainder and that these digits are generated from LSB to MSB, requiring them to be printed in reverse order. After implementing the manual solution, mention alternative approaches such as using a Stack, a StringBuilder, or Java's Integer.toBinaryString() method, along with their trade-offs. This demonstrates both algorithmic understanding and knowledge of Java's standard library.