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

Converting numbers between different bases is a fundamental concept in computer science and programming. Octal (base-8) and decimal (base-10) number systems are commonly used, and converting between them is a useful skill. In this article, we will explore Kotlin Program to Convert Octal Number to Decimal and Vice-Versa with three different solutions and their corresponding outputs.

1. Introduction to Number Conversion

The octal number system uses digits from 0 to 7, 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 Octal 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.

Kotlin
fun octalToDecimal(octal: String): Int {
    return octal.toInt(8)
}

fun main() {
    val octalNumber = "127"
    val decimalNumber = octalToDecimal(octalNumber)
    println("Octal: $octalNumber")
    println("Decimal: $decimalNumber")
}

Output

Kotlin
Octal: 127
Decimal: 87

Explanation

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

2.2. Example 2: Manual Conversion

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

Program

Kotlin
fun manualOctalToDecimal(octal: String): Int {
    var decimal = 0
    var power = 1

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

    return decimal
}

fun main() {
    val octalNumber = "127"
    val decimalNumber = manualOctalToDecimal(octalNumber)
    println("Octal: $octalNumber")
    println("Decimal: $decimalNumber")
}

Output

Kotlin
Octal: 127
Decimal: 87

Explanation

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

2.3. Example 3: Using Recursion

Recursion can also be used to convert an octal number to a decimal number.

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

fun main() {
    val octalNumber = "127"
    val decimalNumber = recursiveOctalToDecimal(octalNumber)
    println("Octal: $octalNumber")
    println("Decimal: $decimalNumber")
}

Output

Kotlin
Octal: 127
Decimal: 87

Explanation

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

3. Conversion from Decimal to Octal

3.1. Example 1: Using Built-in Functions

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

Program

Kotlin
fun decimalToOctal(decimal: Int): String {
    return decimal.toString(8)
}

fun main() {
    val decimalNumber = 87
    val octalNumber = decimalToOctal(decimalNumber)
    println("Decimal: $decimalNumber")
    println("Octal: $octalNumber")
}

Output

Kotlin
Decimal: 87
Octal: 127

Explanation

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

3.2. Example 2: Manual Conversion

We can also manually convert a decimal number to an octal number.

Program

Kotlin
fun manualDecimalToOctal(decimal: Int): String {
    var num = decimal
    val octal = StringBuilder()

    while (num > 0) {
        octal.insert(0, num % 8)
        num /= 8
    }

    return octal.toString()
}

fun main() {
    val decimalNumber = 87
    val octalNumber = manualDecimalToOctal(decimalNumber)
    println("Decimal: $decimalNumber")
    println("Octal: $octalNumber")
}

Output

Kotlin
Decimal: 87
Octal: 127

Explanation

In this example, the manualDecimalToOctal function repeatedly divides the decimal number by 8 and prepends the remainder to a StringBuilder. This constructs the octal 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 an octal number.

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

fun main() {
    val decimalNumber = 87
    val octalNumber = recursiveDecimalToOctal(decimalNumber)
    println("Decimal: $decimalNumber")
    println("Octal: $octalNumber")
}

Output

Kotlin
Decimal: 87
Octal: 127

Explanation

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

4. Conclusion

Converting numbers between octal 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 octal numbers to decimal and vice versa using Kotlin.