Program to Reverse a Number using Recursion in Kotlin

In this article, we will learn to write Kotlin program to Reverse a Number using Recursion.

Source Code:
import java.util.Scanner

fun main(args: Array<String>) {
    var n: Int
    var reverse = 0

    println("Enter a number to reverse")
    val input = Scanner(System.`in`)
    n = input.nextInt()

    while (n != 0) {
        reverse = reverse * 10
        reverse = reverse + n % 10
        n = n / 10

    println("Reverse of the number is $reverse")
Enter a number to reverse
Reverse of the number is 413121
In the above program to reverse number, we have taken input from a user to enter the number. Next, to find the reverse of entered number, we have used recursion using while loop. As you can see in the output, program reverse the number entered.

Post a Comment