Decimal To Binary Java

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

Here in this java program user will be asked to enter decimal number to convert it into binary and display it. In the previous post we learnt how to convert binary number to decimal number.

In the below example we will be using Scanner class of java.util package. I’m going to show you how exactly decimal number 20 is converted to 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). Finally here’s the java program to convert decimal to binary,

decimal to binary java

Example: decimal to binary java

public class DecimalToBinaryDemo
{
   // this function to converts decimal to binary 
   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

 


Java program to convert decimal to binary 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


Java program to convert decimal to binary without predefined method

Here we are going to learn convert decimal to binary java without predefined method. Let’s see an 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


Convert decimal to binary in java using stack class

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

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


Java program to convert decimal to binary and counting number of 1’s

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 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.

Related Posts