Kotlin Program to calculate the power using recursion

Kotlin Program to calculate the power using recursion?Recursion is a powerful technique, it allows us to define a complex problem in terms of solving a simpler version.

Kotlin Program to calculate the power using recursion

Source Code

fun main(args: Array<String>) {
    val base = 3
    val powerRaised = 4
    val result = power(base, powerRaised)

    println("$base^$powerRaised = $result")
}

fun power(base: Int, powerRaised: Int): Int {
    if (powerRaised != 0)
        return base * power(base, powerRaised - 1)
    else
        return 1
}

Output of Program

Kotlin Program to calculate the power using recursion
Kotlin Program to calculate the power using recursion