Convert int to binary java

Hey guys!! Welcome to flower brackets blog. Today we are going to learn convert int to binary java.

convert int to binary java

import java.util.Scanner;

public class IntToBinary
{
   public static void main(String[] args)
   {
      int number = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter an integer : ");
      number = sc.nextInt(); 
      convertToBinary(number);
      sc.close();
   }
 
   public static void convertToBinary(int num)
   {
      String str = "";
      for(int a = 0; a < 8; a++)
      {
         if(num % 2 == 1)
         {
            str = "1" + str;
         }
         if(num % 2 == 0)
         {
            str = "0" + str;
         }
         num = num / 2;
      }
      System.out.println("The binary conversion is : " + str);
   }
}

Output:

Please enter an integer : 14
The binary conversion is : 00001110


Using while loop

In this segment we are going to learn how to convert integer to binary in java using while loop. So let’s see the code implementation,

import java.util.Scanner;

public class IntegerToBinary
{
   public static void main(String[] args)
   {
      int num;
      String str = "";
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter the a number : ");
      num = sc.nextInt();
      while(num > 0)
      {
         int y = num % 2;
         str = y + str;
         num = num / 2;
      }
      System.out.println("The binary conversion is : " + str);
      sc.close();
   }
}

Output:

Please enter the a number : 23
The binary conversion is : 10111


Using Integer.toBinaryString()

Here we are going to learn binary conversion java using Integer.toBinaryString() and also we will be counting number of one bits in the given integer.

Also read – binary to decimal java

import java.util.Scanner;

public class IntToBinaryDemo
{
   public static void main(String[] args)
   {
      int number;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter the a number : ");
      number = sc.nextInt(); 
      System.out.println("The Binary conversion is : " + Integer.toBinaryString(number)); 
      System.out.println("The number of 1 bits in " + number + " is : "+ Integer.bitCount(number));
      sc.close();
   }
}

Output:

Please enter the a number : 23
The Binary conversion is : 10111
The number of 1 bits in 23 is : 4


Without using method

Let’s see an example on how to convert integer to binary in java without using method,

public class ToBinaryJava 
{
   public static void main(String[] args) 
   {
      ToBinaryJava tbj = new ToBinaryJava();
      System.out.println("Binary conversion java of 113 : ");
      tbj.convertToBinary(113);
      System.out.println("\nBinary conversion java of 41 : ");
      tbj.convertToBinary(41);
      System.out.println("\nBinary conversion java of 555 : ");
      tbj.convertToBinary(555);
   }

   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]);
      }
   }
}

Output :

Binary conversion java of 113 : 1110001
Binary conversion java of 41 : 101001
Binary conversion java of 555 : 1000101011

Related Posts