Related Articles
Java Program to Convert Integer Values into Binary
• Last Updated : 29 Oct, 2020

Integers are the numbers whose base value is 10.  A binary number is a number expressed in the base-2 numeral. Binary consists of only 2 digits 0 and 1. Two operators are used in the conversion of integers to binary modulo and division to convert the given input into binary numbers.

Example:

```Input:  = 45
Output: = 101101

Input:  = 32
Output: = 100000
```

There are many approaches to convert an integer into binary numbers some of them are discussed here. We will be discussing two of them:

• Using Implementation of Stack
• Using Inbuilt Method- toBinaryString() of the Integer class of Java

Approach 1: Using Implementation of Stack

Actually, the binary number consists of only 0 and 1.  To convert an integer to binary divide the number by 2 until it becomes 0. In each step take the modulo by 2 and store the remainder into an array or stack. If we store the remainder into an array then print it into reverse order. If we store the remainder into stack then simply pop one by one element and print it.

Below is the java implementation of the above approach:

## Java

 `// Java Program to Convert Integer Values into Binary ` ` `  `// Importing CLasses/Files ` `import` `java.io.*; ` ` `  `public` `class` `GFG { ` `    ``// Function to print binary number ` `    ``static` `void` `printBinary(``int``[] binary, ``int` `id) ` `    ``{ ` `        ``// Iteration over array ` `        ``for` `(``int` `i = id - ``1``; i >= ``0``; i--) ` `            ``System.out.print(binary[i] + ``""``); ` `    ``} ` ` `  `    ``// Function convering decimal to binary ` `    ``public` `static` `void` `decimalToBinary(``int` `num) ` `    ``{ ` `        ``// Creating and assigning binary array size ` `        ``int``[] binary = ``new` `int``[``35``]; ` `        ``int` `id = ``0``; ` ` `  `        ``// Number should be positive ` `        ``while` `(num > ``0``) { ` `            ``binary[id++] = num % ``2``; ` `            ``num = num / ``2``; ` `        ``} ` ` `  `        ``// Print Binary ` `        ``printBinary(binary, id); ` `    ``} ` ` `  `    ``// Main Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// Entered number to be convert into binary ` `        ``int` `num = ``45``; ` ` `  `        ``// Calling Our Above Function ` `        ``decimalToBinary(num); ` `    ``} ` `}`

Output

```101101
```

Using stack by creating object vector

## Java

 `// Java Program to Convert Integer Values into Binary ` ` `  `// Importing Classes/Files ` `import` `java.io.*; ` `import` `java.util.Stack; ` ` `  `public` `class` `GFG { ` `   `  `    ``// Function to covert integer to binary ` `    ``static` `void` `decimalToBinary(``int` `num) ` `    ``{ ` `        ``// Creating Stack Object Vector ` `        ``Stack st = ``new` `Stack<>(); ` ` `  `        ``// Number Should be positive ` `        ``while` `(num > ``0``) { ` `           `  `            ``// Pushing numbers inside stack that ` `            ``// are divisible by 2 ` `            ``st.push(num % ``2``); ` `            ``// Dividing number by 2 ` `            ``num = num / ``2``; ` `        ``} ` ` `  `        ``// Checking condition whether stack is empty ` `        ``while` `(!(st.isEmpty())) { ` `           `  `            ``// Printing binary number ` `            ``System.out.print(st.pop()); ` `        ``} ` `    ``} ` ` `  `    ``// Main driver function ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// Entered number to be converted into binary ` `        ``int` `num = ``45``; ` `        ``decimalToBinary(num); ` `    ``} ` `}`

Output

```101101
```

Approach 2: Using toBinaryString() inbuilt method of the Integer class of Java

## Java

 `// Java Program to Convert Integer Values into Binary ` ` `  `// Importing Classes/Files ` `import` `java.io.*; ` ` `  `class` `GFG { ` `    ``// Function converting decimal to binary ` `    ``static` `void` `decimalToBinary(``int` `num) ` `    ``{ ` `        ``// Function to print integer to binary using ` `        ``// inbuilt toBinaryString method ` `        ``System.out.println(Integer.toBinaryString(num)); ` `    ``} ` ` `  `    ``// Main driver function ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// Number to be converted into binary ` `        ``int` `num = ``45``; ` ` `  `        ``// Calling function ` `        ``decimalToBinary(num); ` `    ``} ` `}`

Output

`101101`

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :