Decimal To Binary Java

Hey guys!! Welcome to flower brackets blog. Today in this post we will learn decimal to binary java.

Here user will be asked to enter decimal number to convert it into decimal number and display it.

In the previous post we learnt how to convert binary number to decimal number java example.

In the below example we will be using Scanner class of java.util package.

For example, decimal number 20 –

I’m going to show you how exactly decimal number 20 is converted binary. So you have the number 20 divide that by 2 you get quotient 10 and remainder 0.

Now at this point we get one binary digit that is 0 at the end as the remainder.

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

Again take quotient 5 repeat the same thing 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.

In the last step take the quotient as 1 and as you can see the remainder is going to be 1.

Now, collecting all the remainders you get 00101. Actually this is not the binary number.

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

decimal to binary java

Example: decimal to binary java

import java.util.Scanner;

public class DecimalToBinary
{
   public String toBinary(int a)
   {
      if(a == 0)
      {
         return "0";
      }
      String strBinary = "";
      while(a > 0)
      {
         int b = a % 2;
         strBinary = b + strBinary;
         a = a / 2;
      }
      return strBinary;
   }

   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number to convert: ");
      int decimal = sc.nextInt();

      DecimalToBinary dtb = new DecimalToBinary();
      String str = dtb.toBinary(decimal);
      System.out.println("Converted binary value is " + str);
   }
}

Output:

Convert Decimal to Binary Java Example


Using toBinaryString method

Let’s see how to convert a number to binary in java using toBinaryString method,

public class DecimalToBinary
{
   public static void main(String[] args)
   {
      System.out.println("Decimal to binary java of 104: ");
      System.out.println(Integer.toBinaryString(104));
      System.out.println("\nDecimal to binary java of 554: ");
      System.out.println(Integer.toBinaryString(554));
      System.out.println("\nDecimal to binary java of 644: ");
      System.out.println(Integer.toBinaryString(644));
   }
}

Output:

Decimal to binary java of 104: 1101000

Decimal to binary java of 554: 1000101010

Decimal to binary java of 644: 1010000100


Without predefined method

Here we are going to learn convert decimal to binary java without predefined method. Let’s see example,

public class DecimalToBinaryDemo
{
   public void convertToBinary(int number)
   {
      int toBinary[] = new int[40];
      int index = 0;
      while(number > 0)
      {
         toBinary[index++] = number % 2;
         number = number / 2;
      }
      for(int a = index - 1; a >= 0; a--)
      {
         System.out.print(toBinary[a]);
      }
   }
 
   public static void main(String[] args)
   {
      DecimalToBinaryDemo dtb = new DecimalToBinaryDemo();
      System.out.println("Decimal to binary java of 104: ");
      dtb.convertToBinary(104);
      System.out.println("\nDecimal to binary java of 554: ");
      dtb.convertToBinary(554);
      System.out.println("\nDecimal to binary java of 644: ");
      dtb.convertToBinary(999);
   }
}

Output:

Decimal to binary java of 104: 1101000
Decimal to binary java of 554: 1000101010
Decimal to binary java of 644: 1111100111


Using Stack

Now let’s see how to convert decimal to binary in java using stack.

import java.util.*;

public class DecimalBinaryExample
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in); 
      Stack<Integer> numStack = new Stack<Integer>(); 
      System.out.println("Please enter a decimal number : ");
      int number = sc.nextInt();
 
      while (number != 0)
      {
         int a = number % 2;
         numStack.push(a);
         number /= 2;
      } 
 
      System.out.print("\nDecimal to binary conversion java : ");
      while(!(numStack.isEmpty()))
      {
         System.out.print(numStack.pop());
      }
      System.out.println();
      sc.close();
   }
}

Output:

Please enter a decimal number : 226

Decimal to binary conversion java : 11100010


Counting number of 1’s in decimal to binary java

Let’s see how to convert a number to binary in java and counting number of 1s,

import java.util.Scanner;

public class DecimalBinaryDemo
{
   public static void main(String[] args)
   {
      int number, count = 0, temp;
      String strConvert = "";
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter a decimal number : ");
      number = sc.nextInt();
      while(number > 0)
      {
         temp = number % 2;
         if(temp == 1)
         {
            count++;
         }
         strConvert = strConvert + " " + temp;
         number = number / 2;
      }
      System.out.println("Decimal to binary java : " + strConvert);
      System.out.println("Number of 1s : " + count);
      sc.close();
   }
}

Output :

Please enter a decimal number : 266
Decimal to binary java : 0 1 0 1 0 0 0 0 1
Number of 1s : 3


conclusion

So this is all about how to convert decimal to binary in java with an explanation and example.

I hope you guys have understood the concept about java program to convert decimal to binary.

You can subscribe to my blog flower brackets, if you haven’t already.

Do share this article if you like.

You May Also Like