Kotlin Program to convert the number into characters


In this program, You will learn how to convert the number into characters in Kotlin.


Number is: 12345

One Two Three Four Five

Example: How to convert the number into characters in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var n: Int
    var r: Int
    var rev = 0

    var sc = Scanner(System.`in`)

    print("Enter a number:")
    n = sc.nextInt()

    while (n > 0) {
        r = n % 10
        rev = rev * 10 + r
        n /= 10
    }
    n = rev


    while (n > 0) {
        r = n % 10
        when (r) {
            0 -> print("Zero ")

            1 -> print("One ")

            2 -> print("Two ")

            3 -> print("Three ")

            4 -> print("Four ")

            5 -> print("Five ")

            6 -> print("Six ")

            7 -> print("Seven ")

            8 -> print("Eight ")

            9 -> print("Nine ")
        }
        n /= 10
    }
}

Output:

Enter a number:2903
Two Nine Zero Three