0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Convert Decimal to Binary

You can convert a decimal number to binary in Kotlin using Integer.toBinaryString(decimalNumber) or by repeatedly dividing the number by 2 and collecting remainders.
📋

Examples

Input0
Output0
Input10
Output1010
Input255
Output11111111
🧠

How to Think About It

To convert a decimal number to binary, think of dividing the number by 2 repeatedly and noting the remainder each time. These remainders, read in reverse order, form the binary number. Alternatively, Kotlin provides a built-in function to do this conversion easily.
📐

Algorithm

1
Get the decimal number as input.
2
If the number is 0, return "0" as binary.
3
While the number is greater than 0, divide it by 2 and store the remainder.
4
Collect all remainders in reverse order to form the binary string.
5
Return the binary string.
💻

Code

kotlin
fun main() {
    val decimalNumber = 10
    val binaryString = Integer.toBinaryString(decimalNumber)
    println("Binary of $decimalNumber is $binaryString")
}
Output
Binary of 10 is 1010
🔍

Dry Run

Let's trace the decimal number 10 through the code using the manual division method.

1

Initial number

decimalNumber = 10

2

Divide by 2 and get remainder

10 / 2 = 5 remainder 0

3

Divide quotient by 2 and get remainder

5 / 2 = 2 remainder 1

4

Repeat division

2 / 2 = 1 remainder 0

5

Repeat division

1 / 2 = 0 remainder 1

6

Collect remainders in reverse

Remainders collected: 0,1,0,1 reversed to 1010

QuotientRemainder
100
51
20
11
💡

Why This Works

Step 1: Using built-in function

The Integer.toBinaryString() function converts the decimal number directly to its binary string representation.

Step 2: Manual conversion logic

Dividing the number by 2 and collecting remainders simulates how binary numbers are formed, with each remainder representing a binary digit.

Step 3: Reversing remainders

The binary digits are collected from least significant to most significant bit, so reversing the remainders gives the correct binary number.

🔄

Alternative Approaches

Manual division and string building
kotlin
fun decimalToBinary(num: Int): String {
    if (num == 0) return "0"
    var number = num
    val binary = StringBuilder()
    while (number > 0) {
        binary.append(number % 2)
        number /= 2
    }
    return binary.reverse().toString()
}

fun main() {
    val decimalNumber = 10
    println("Binary of $decimalNumber is ${decimalToBinary(decimalNumber)}")
}
This method shows the step-by-step logic but is longer than using the built-in function.
Using Kotlin extension function
kotlin
fun Int.toBinary(): String = Integer.toBinaryString(this)

fun main() {
    val decimalNumber = 10
    println("Binary of $decimalNumber is ${decimalNumber.toBinary()}")
}
This approach wraps the built-in function in an extension for cleaner code.

Complexity: O(log n) time, O(log n) space

Time Complexity

The algorithm divides the number by 2 repeatedly until it reaches 0, which takes about log base 2 of n steps.

Space Complexity

The space needed is proportional to the number of binary digits, which is also about log base 2 of n.

Which Approach is Fastest?

Using the built-in Integer.toBinaryString() is fastest and simplest, while manual methods are educational but slower.

ApproachTimeSpaceBest For
Built-in functionO(log n)O(log n)Quick and simple conversion
Manual divisionO(log n)O(log n)Learning and understanding binary conversion
Extension functionO(log n)O(log n)Cleaner syntax using built-in method
💡
Use Kotlin's built-in Integer.toBinaryString() for quick and reliable decimal to binary conversion.
⚠️
Beginners often forget to reverse the collected remainders, resulting in the binary number being backwards.