In this tutorial, you shall learn how to reverse a given number in Kotlin, with example programs.

Kotlin – Reverse a number

To reverse a number in Kotlin,

  1. Convert given number to string using toString() method.
  2. Reverse this string using String.reversed() method.
  3. Convert the reversed string to integer using String.toInt() method.

Program

In the following program, we read a number n from user and reverse it using the above steps.

Main.kt

</>
Copy
fun main() {
    print("Enter an integer : ")
    val n = readLine()!!.toInt()

    val reversed = n.toString().reversed().toInt()
    
    println("Original : $n")
    println("Reversed : $reversed")
}

Output #1

Enter an integer : 123456
Original : 123456
Reversed : 654321

Output #2

Enter an integer : 11002233
Original : 11002233
Reversed : 33220011

Related tutorials for the above program

Conclusion

In this Kotlin Tutorial, we learned how to reverse a given integer number.