Java program to convert decimal to binary using toBinaryString and stack

Let’s learn java program to convert decimal to binary using toBinaryString and stack.

Java program to convert decimal to binary using toBinaryString and stack

Convert decimal to binary in java is the most common java interview question.

There are two ways in converting decimal to binary in java, one using inbuilt function toBinaryString() method of Integer class and another using stack.

java program to convert decimal to binary using toBinaryString and stack

Convert decimal to binary using toBinaryString() method

public class DecimalToBinary
{
   public static void main(String[] args)
   {
      System.out.println("decimal to binary using toBinaryString() 104: ");
      // to binary string java
      System.out.println(Integer.toBinaryString(104));
      System.out.println("\ndecimal to binary using toBinaryString() 554: ");
      // tobinarystring
      System.out.println(Integer.toBinaryString(554));
      System.out.println("\ndecimal to binary using toBinaryString() 644: ");
      System.out.println(Integer.toBinaryString(644));
   }
}


 

Output:

decimal to binary using toBinaryString() 104: 1101000

decimal to binary using toBinaryString() 554 : 1000101010

decimal to binary using toBinaryString() 644 : 1010000100


Convert decimal to binary using stack in java

Similarly here’s the java program to convert decimal to binary 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.println("Binary number : ");
      while(!(numStack.isEmpty()))
      {
         System.out.print(numStack.pop());
      }
      System.out.println();
      sc.close();
   }
}


Output:

Please enter a decimal number : 665
Binary number : 1010011001


Also read – access modifiers in java