beginnerNumber ProblemsJava

Convert a Decimal Number to Binary

Convert a base-10 integer to its binary representation using arithmetic.

Quick Answer

Repeatedly divide the number by 2, collecting each remainder (0 or 1). The remainders are the binary digits in reverse order, so read them from last to first. Keep dividing until the number becomes 0, and handle 0 itself as the special case that prints '0'.

Problem Statement

Given a non-negative integer, return its binary representation as a string of 0s and 1s, without using a built-in radix converter like Integer.toBinaryString.

Binary is base 2, so each position is a power of two. For example 10 in decimal is 1010 in binary because 10 = 8 + 2.

Input: A single non-negative integer n.

Output: A string of 0s and 1s giving n in base 2.

Examples

Example 1
Input:  10
Output: 1010

10 = 8 + 2 = 1010 in binary.

Example 2
Input:  13
Output: 1101

13 = 8 + 4 + 1 = 1101 in binary.

Constraints

  • 0 <= n <= 2,147,483,647
  • No Integer.toBinaryString or similar library shortcut

Think Before You Code

Reveal the questions to ask yourself first
  • What does the remainder of a number divided by 2 tell you about its last binary bit?
  • In which order do the remainders come out, and how do you fix that order?
  • Where does dividing by 2 each step leave you, and when do you stop?
  • What binary string should 0 produce?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
n % 2 gives the least significant binary digit; n / 2 removes it, just like % 10 and / 10 do for decimal digits.
Hint 2
Collect the remainders while n > 0. They come out least-significant first, so prepend each new bit (or reverse at the end).
Hint 3
Handle n = 0 up front by returning "0", because the loop body never runs for it.

Approach

Reveal the step-by-step approach

Use repeated division by the base, exactly as you would to peel decimal digits.

  1. If n is 0, return "0" directly.
  2. While n > 0:
    • bit = n % 2 — the current least significant binary digit.
    • Prepend bit to the growing result.
    • n = n / 2 — shift right by one binary place.
  3. Return the accumulated string of bits.

Prepending each new bit keeps the most significant bit (found last) at the front, giving the correct left-to-right binary order.

Dry Run

Walk through the example step by step

Converting n = 13 to binary:

step | n  | bit = n % 2 | result (prepended)
-----+----+-------------+-------------------
1    | 13 | 1           | 1
2    | 6  | 0           | 01
3    | 3  | 1           | 101
4    | 1  | 1           | 1101
end  | 0  | —           | 1101

Solution

Reveal the full Java solution
public class DecimalToBinary {
    public static String toBinary(int n) {
        if (n == 0) {
            return "0";
        }
        StringBuilder bits = new StringBuilder();
        int value = n;
        while (value > 0) {
            int bit = value % 2;
            bits.insert(0, bit);
            value /= 2;
        }
        return bits.toString();
    }

    public static void main(String[] args) {
        System.out.println(toBinary(10)); // 1010
        System.out.println(toBinary(13)); // 1101
    }
}

Dividing by 2 and recording the remainder is the general base-conversion recipe; swapping the 2 for any base b converts to that base. Each remainder is one bit, produced from least significant to most significant, so inserting every new bit at position 0 rebuilds the number in normal reading order without a separate reverse step.

Time: O(log n) — one step per binary digitSpace: O(log n) for the output string

Common Mistakes

  • Appending bits to the end, which yields the binary digits reversed.
  • Forgetting the n = 0 case and returning an empty string.
  • Using Integer.toBinaryString, which defeats the purpose of the exercise.

Edge Cases to Test

  • n = 0 must return "0".
  • Powers of two like 8 produce a single 1 followed by zeros (1000).
  • Large values such as 2147483647 produce a 31-bit string of all 1s.

Interview Follow-Ups

  • How would you convert to hexadecimal or any base b with the same loop?
  • How would you handle negative numbers using two's-complement representation?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us