Kotlin Program to Convert Binary Number to Decimal and Vice-Versa

Converting numbers between different bases is a common task in computer science. Binary (base-2) and decimal (base-10) are two of the most frequently used number systems. In this article, we will explore Kotlin Program to Convert Binary Number to Decimal and Vice-Versa with three different solutions and their corresponding outputs.

1. Introduction to Number Conversion

The binary number system uses digits 0 and 1, while the decimal system uses digits from 0 to 9. Conversion between these systems involves understanding how to interpret the value of each digit based on its position.

2. Conversion from Binary to Decimal

2.1. Example 1: Using Built-in Functions

Kotlin provides built-in functions to convert between different number systems, making it easy to implement these conversions.

Program

Kotlin
fun binaryToDecimal(binary: String): Int {
    return binary.toInt(2)
}

fun main() {
    val binaryNumber = "1011"
    val decimalNumber = binaryToDecimal(binaryNumber)
    println("Binary: $binaryNumber")
    println("Decimal: $decimalNumber")
}

Output

Kotlin
Binary: 1011
Decimal: 11

Explanation

In this example, the binaryToDecimal function uses Kotlin’s toInt method with a radix of 2 to convert a binary string to a decimal integer. The main function demonstrates this conversion by printing both the binary and decimal numbers.

2.2. Example 2: Manual Conversion

For a deeper understanding, we can manually convert a binary number to a decimal number.

Program

Kotlin
fun manualBinaryToDecimal(binary: String): Int {
    var decimal = 0
    var power = 1

    for (i in binary.length - 1 downTo 0) {
        decimal += (binary[i] - '0') * power
        power *= 2
    }

    return decimal
}

fun main() {
    val binaryNumber = "1011"
    val decimalNumber = manualBinaryToDecimal(binaryNumber)
    println("Binary: $binaryNumber")
    println("Decimal: $decimalNumber")
}

Output

Kotlin
Binary: 1011
Decimal: 11

Explanation

In this example, the manualBinaryToDecimal function iterates through each digit of the binary number from right to left. It calculates the decimal value by multiplying each digit by the appropriate power of 2 and summing the results.

2.3. Example 3: Using Recursion

Recursion can also be used to convert a binary number to a decimal number.

Program

Kotlin
fun recursiveBinaryToDecimal(binary: String, index: Int = binary.length - 1, power: Int = 1): Int {
    return if (index < 0) {
        0
    } else {
        (binary[index] - '0') * power + recursiveBinaryToDecimal(binary, index - 1, power * 2)
    }
}

fun main() {
    val binaryNumber = "1011"
    val decimalNumber = recursiveBinaryToDecimal(binaryNumber)
    println("Binary: $binaryNumber")
    println("Decimal: $decimalNumber")
}

Output

Binary: 1011
Decimal: 11

Explanation

In this example, the recursiveBinaryToDecimal function uses recursion to convert a binary number to a decimal number. It multiplies each digit by the appropriate power of 2 and sums the results. The main function demonstrates this conversion.

3. Conversion from Decimal to Binary

3.1. Example 1: Using Built-in Functions

Kotlin provides built-in functions to convert a decimal number to a binary string.

Program

Kotlin
fun decimalToBinary(decimal: Int): String {
    return decimal.toString(2)
}

fun main() {
    val decimalNumber = 11
    val binaryNumber = decimalToBinary(decimalNumber)
    println("Decimal: $decimalNumber")
    println("Binary: $binaryNumber")
}

Output

Kotlin
Decimal: 11
Binary: 1011

Explanation

In this example, the decimalToBinary function uses Kotlin’s toString method with a radix of 2 to convert a decimal integer to a binary string. The main function demonstrates this conversion by printing both the decimal and binary numbers.

3.2. Example 2: Manual Conversion

We can also manually convert a decimal number to a binary number.

Program

Kotlin
fun manualDecimalToBinary(decimal: Int): String {
    var num = decimal
    val binary = StringBuilder()

    while (num > 0) {
        binary.insert(0, num % 2)
        num /= 2
    }

    return binary.toString()
}

fun main() {
    val decimalNumber = 11
    val binaryNumber = manualDecimalToBinary(decimalNumber)
    println("Decimal: $decimalNumber")
    println("Binary: $binaryNumber")
}

Output

Kotlin
Decimal: 11
Binary: 1011

Explanation

In this example, the manualDecimalToBinary function repeatedly divides the decimal number by 2 and prepends the remainder to a StringBuilder. This constructs the binary representation of the number. The main function demonstrates this conversion.

3.3. Example 3: Using Recursion

Recursion can also be used to convert a decimal number to a binary number.

Program

Kotlin
fun recursiveDecimalToBinary(decimal: Int): String {
    return if (decimal < 2) {
        decimal.toString()
    } else {
        recursiveDecimalToBinary(decimal / 2) + (decimal % 2).toString()
    }
}

fun main() {
    val decimalNumber = 11
    val binaryNumber = recursiveDecimalToBinary(decimalNumber)
    println("Decimal: $decimalNumber")
    println("Binary: $binaryNumber")
}

Output

Kotlin
Decimal: 11
Binary: 1011

Explanation

In this example, the recursiveDecimalToBinary function uses recursion to convert a decimal number to a binary number. It divides the decimal number by 2 and appends the remainder to the result of the recursive call. The main function demonstrates this conversion.

4. Conclusion

Converting numbers between binary and decimal systems can be done using various methods in Kotlin:

  1. Using Built-in Functions: Simplifies the conversion process with Kotlin’s built-in methods.
  2. Manual Conversion: Provides a deeper understanding of the conversion logic.
  3. Recursion: Demonstrates a functional approach to conversion.

Each method has its advantages, and choosing the right one depends on your specific needs and preferences. By following these examples, you can effectively convert binary numbers to decimal and vice versa using Kotlin.