Check Palindrome number in Kotlin

A palindrome number is a number that remains the same when its digits are reversed. For example, 121, 1221, and 12321 are palindrome numbers.

Kotlin
fun isPalindrome(number: Int): Boolean {
    val numberString = number.toString()
    return numberString == numberString.reversed()
}

This function takes an integer as input and first converts it to a string using the toString() function. It then checks if the string is equal to its reversed version using the reversed() function. If they are equal, then the number is a palindrome and the function returns true, otherwise it returns false.

Kotlin
fun isPalindrome(number: Int): Boolean {
    val numberString = number.toString()
    return numberString == numberString.reversed()
}
fun main() {
    val number1 = 121
    val number2 = 123

    if (isPalindrome(number1)) {
        println("$number1 is a palindrome")
    } else {
        println("$number1 is not a palindrome")
    }

    if (isPalindrome(number2)) {
        println("$number2 is a palindrome")
    } else {
        println("$number2 is not a palindrome")
    }
}

This program first defines two numbers, number1 and number2. It then checks each number using the isPalindrome() function and prints out whether or not it is a palindrome.

Leave a Comment