# Kotlin program to convert decimal to binary

Kotlin | Convert decimal to binary: Here, we are going to learn how to convert a given decimal number to binary number in Kotlin?
Submitted by IncludeHelp, on April 16, 2020

We are given a decimal number and we will create a Kotlin program to convert decimal number to binary.

To convert decimal number to binary, we will recursively divide the decimal number by 2 and save the remainder found till the decimal number vanquishes.

Example:

```    Input:
Decimal number = 12

Recursive division:

NumberQuotientRemainder
1260
630
311
101

Output:
Here, we will read the remainders in reverse order
which is the binary equivalent i.e.
1100 is the binary conversion for 12.
```

### Program to convert decimal to binary in Kotlin

```package com.includehelp.basic

import java.util.*

/* function to convert given decimal number into Binary */
fun getBinaryNumber(decimalNumber: Int): String {
var decimalNumber = decimalNumber
val binaryStr = StringBuilder()

while (decimalNumber > 0) {
val r = decimalNumber % 2
decimalNumber /= 2
binaryStr.append(r)
}

return binaryStr.reverse().toString()
}

// Main Method Entry Point of Program
fun main(arg: Array<String>) {
val sc = Scanner(System.`in`)

println("Enter Decimal Number  : ")
//Input Decimal Number
val decimalNumber: Int = sc.nextInt()

// Call function to Convert Decimal into binary
val binaryNumber = getBinaryNumber(decimalNumber)
// Print Binary Number
println("Binary Number : \$binaryNumber")
}
```

Output

```Run 1:
Enter Decimal Number  :
15
Binary Number : 1111
-----
Run 2:
Enter Decimal Number  :
12345
Binary Number : 11000000111001
----
Run 3:
Enter Decimal Number  :
545654
Binary Number : 10000101001101110110
```