Convert decimal number to binary – (java api, iterative & recursive)

  • Given a decimal number (base 10), convert decimal integer to binary number (base 2) using java.
  • We will discuss various methods to convert decimal number to binary.
    • Convert decimal to binary using Integer class
      • Integer.toBinaryString
      • Integer.toString
    • Iterative program to convert decimal number to binary number.
    • Recursive method to convert decimal number to binary number.

Methods to convert decimal integer to binary:

1.) Convert integer to binary using Integer.toBinaryString

private static String toBinaryString(int number) {
 return Integer.toBinaryString(number);
}

2.) Convert decimal integer to binary using Integer.toString

private static String toString(int number) {
 return Integer.toString(number, 2);
}

3.) Convert decimal to binary using iterative method

private static String iterative(int number) {
  StringBuilder builder = new StringBuilder();

  while (number > 0) {
   builder.append(number % 2);
   number /= 2;
  }
  return builder.reverse().toString();
}

4.) Convert decimal integer to binary – recursive method

private static String recursive(int number) {
 StringBuilder builder = new StringBuilder();
 
 if (number > 0) {
  String binaryNumber = recursive(number / 2);
  int digit = number % 2;
  builder.append(binaryNumber + digit);
 }
 return builder.toString();
}

Complete: program to convert decimal number to binary

package org.learn;

package org.learn;

import java.util.Scanner;

public class DecimalToBinary {

    public static void main(String[] args) {
        try (Scanner scanner = new Scanner(System.in)) {
            System.out.printf("1. Enter decimal number : ");
            int number = scanner.nextInt();
            String binary = toBinaryString(number);

            System.out.printf("2. Binary of (%d) using toBinaryString method = %s\n", number, binary);

            binary = toString(number);
            System.out.printf("3. Binary of (%d) using toString method = %s\n", number, binary);

            binary = iterative(number);
            System.out.printf("4. Binary of (%d) using iterative method = %s\n", number, binary);

            binary = recursive(number);
            System.out.printf("5. Binary of (%d) using recursive method = %s\n", number, binary);
        }
    }

    private static String toBinaryString(int number) {
        return Integer.toBinaryString(number);
    }

    private static String toString(int number) {
        return Integer.toString(number, 2);
    }

    private static String iterative(int number) {
        StringBuilder builder = new StringBuilder();

        while (number > 0) {
            builder.append(number % 2);
            number /= 2;
        }
        return builder.reverse().toString();
    }

    private static String recursive(int number) {
        StringBuilder builder = new StringBuilder();

        if (number > 0) {
            String binaryNumber = recursive(number / 2);
            int digit = number % 2;
            builder.append(binaryNumber + digit);
        }
        return builder.toString();
    }
}

Program output –  convert decimal integer to binary

1. Enter decimal number : 10
2. Octal of (10) using toOctalString method = 1010
3. Octal of (10) using toString method = 1010
4. Octal of (10) using iterative method = 1010
5. Octal of (10) using recursive method = 1010

1. Enter decimal number : 25
2. Binary of (25) using toBinaryString method = 11001
3. Binary of (25) using toString method = 11001
4. Binary of (25) using iterative method = 11001
5. Binary of (25) using recursive method = 11001
Scroll to Top