Kotlin Program to Calculate the Power of a Number: In this program, base and exponent are assigned values 3 and 4 respectively. Using the while loop, we keep on multiplying the result by base until exponent becomes zero.In this case, we multiply result by base 4 times in total, so result = 1 * 3 * 3 * 3 * 3 = 81.
Kotlin Program to Calculate the Power of a Number
Source Code
fun main(args: Array<String>) {
val base = 3
var exponent = 4
var result: Long = 1
while (exponent != 0) {
result *= base.toLong()
--exponent
}
println("Answer = $result")
}
Output
Kotlin Program to Calculate the Power of a Number