Since Kotlin is a multi-paradigm language, we can also avoid using for loop and use recursion instead…