Java Program to convert from decimal to binary



To convert decimal to binary, Java has a method “Integer.toBinaryString()”. The method returns a string representation of the integer argument as an unsigned integer in base 2.

Let us first declare and initialize an integer variable.

int dec = 25;

Convert it to binary.

String bin = Integer.toBinaryString(dec);

Now display the “bin” string, which consists of the Binary value. Here is the complete example.

Example

 Live Demo

public class Demo {
   public static void main( String args[] ) {
      int dec = 25;
      // converting to binary and representing it in a string
      String bin = Integer.toBinaryString(dec);
      System.out.println(bin);
   }
}

Output

11001
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements