Java program to convert decimal number to binary

Let’s learn java program to convert decimal number to binary.

java program to convert decimal number to binary

Java program to convert decimal number to binary

How to convert a decimal number to binary is one of the frequently asked java program interview question.

To convert decimal to binary in java there few methods. They are,

I’m going to show you to convert decimal number to binary number without using predefined method.

So you have number 20 divide that by 2 to get quotient 10 and remainder 0. Now at this point we get one binary digit, that is 0 as the remainder.

Remember the remainder is what gives you the binary digit. Now take quotient 10 again divide that by 2 and you get the quotient as 5 and remainder as 0.

Again take quotient 5. Divide that by 2 you will get the quotient as 2 and remainder as 1. Repeat the same procedure divide that by 2, you will get quotient as 1 and remainder as 0.

Also read – function to convert decimal to binary in java

Now in the last step quotient is 1 and remainder is 1. Now, collecting all the remainders, you get 00101. Actually this is not the binary number.

We have to read reverse, that is, 10100 (we have to read it from right to left).

In the below java program we have written function to convert decimal to binary in java.

Also read – java program to convert decimal to binary using for loop

In the below function first we store binary number in an integer array. Then set counter for binary array in while loop.

In while loop store remainder in binary array. Lastly print binary in reverse order.

Here’s the java program to convert decimal to binary,

import java.util.*;

public class DecimalToBinaryDemo
{
   // function to convert decimal to binary in java
   static void toBinary(int num) 
   { 
      // here we are storing binary number 
      int binaryNumber[] = new int[1000]; 
   
      // "count" variable is counter for binary array 
      int count = 0; 
      while(num > 0)  
      { 
         // storing remainder in binary array 
         binaryNumber[count] = num % 2; 
         num = num / 2; 
         count++; 
      } 
   
      // here we are printing binary in reverse order 
      for(int a = count - 1; a >= 0; a--) 
         System.out.print(binaryNumber[a]); 
   } 
 
   public static void main(String[] args)
   {
      int number = 20; 
      toBinary(number); 
   }
}


Output:

10100


Click here to learn java program to convert decimal to binary and vice versa.


Without using array

Let’s learn how to convert decimal to binary without using arrays. For this we are using StringBuilder class.

Also read – java program to convert decimal to hexadecimal

Now let’s see java program for decimal to binary conversion,

public class DecimalToBinaryDemo 
{
   static void toBinary(int num) 
   { 
      StringBuilder sb = new StringBuilder(); 
      int a = 0;
      while(num > 0)
      {
         sb.append(num % 2);
         a++;
         num = num / 2;
      }
      System.out.println(sb.reverse()); 
   }

   public static void main(String[] args) 
   {
      int number = 20; 
      toBinary(number);
   }
}


Output:

10100


Also read – java program to convert decimal to octal

Also read – decimal to binary in java inbuilt function


java program to convert decimal to binary using recursion

Let’s learn java program to convert decimal to binary using recursion,

public class DecimalToBinaryUsingRecursion
{
   public static void main(String[] args)
   {
      int inputDecimal = 50;
      System.out.println(toBinary(inputDecimal));
   }
   static int toBinary(int decimal)
   {
      if(decimal == 0)
         return 0;
      else
         return(decimal % 2 + 10 * toBinary(decimal / 2));
   }
}


Output:

110010