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.

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

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


Also read – java program to convert decimal to binary and vice versa

Also read – decimal to binary in java using recursion

Also read – java program to convert decimal to binary without using array


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


Function to convert decimal to binary in java

public class FunctionDecimalToBinary
{
   public static void main(String[] args) 
   {
      int num = 104;
      functionToBinary(num);
   }
   static void functionToBinary(int number) 
   { 
      int[] binary = new int[1000]; 
      int a = 0;
      while(number > 0)
      { 
         binary[a] = number % 2; 
         number = number / 2; 
         a++; 
      }
      // printing binary number in reverse order
      System.out.println("Function to convert decimal to binary in java: ");
      for(int b = a - 1; b >= 0; b--)
      {
         System.out.print(binary[b]);
      }
   }
}


Output:

Function to convert decimal to binary in java: 1101000


Java program to convert decimal to binary using for loop

import java.util.Scanner;
public class DecimalToBinaryForLoop 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a decimal number: ");
      int inputNumber = sc.nextInt();
      System.out.println("Convert decimal to binary using for loop: " + convertToBinary(inputNumber));
      sc.close();
   }
   public static long convertToBinary(int number)
   {
      long binaryNumber = 0;
      int remainder, a;
      for(a = 1; number != 0; a = a * 10)
      {
         remainder = number % 2;
         number /= 2;
         binaryNumber += remainder * a;
      }
      return binaryNumber;
   }
}


Output:

Please enter a decimal number: 554
Convert decimal to binary using for loop: 1000101010