Java Program to convert an integer into binary

Java 8Object Oriented ProgrammingProgramming

To convert an integer to binary, use the Integer.toBinaryString() method in Java.

Let’s say the following is the integer we want to convert.

int val = 999;

Converting the above integer to binary.

Integer.toBinaryString(val)

Example

 Live Demo

public class Demo {
    public static void main(String[] args) {
       int val = 999;
       // integer
       System.out.println("Integer: "+val);
       // binary
       System.out.println("Binary = " + Integer.toBinaryString(val));
    }
}

Output

Integer: 999
Binary = 1111100111
raja
Published on 12-Dec-2018 06:38:22
Advertisements