Kotlin Program to Check Whether a Number is Palindrome or Not

A palindrome is a number that reads the same backward as forward. Checking whether a number is a palindrome is a common programming problem that can be solved using various methods in Kotlin. This article explores three different Kotlin Program to Check Whether a Number is Palindrome or No explaining each example and providing the output for clarity.

Prerequisites

Before diving into the examples, ensure you have the following prerequisites:

1. Using String Reversal

One straightforward method to check if a number is a palindrome is by converting it to a string, reversing the string, and then comparing the reversed string to the original.

1.2 Program

Kotlin
fun main() {
    val number = 121
    val originalString = number.toString()
    val reversedString = originalString.reversed()
    
    if (originalString == reversedString) {
        println("$number is a palindrome.")
    } else {
        println("$number is not a palindrome.")
    }
}

1.3 Output

Kotlin
121 is a palindrome.

In this example, the number is converted to a string and then reversed. The reversed string is compared to the original string to determine if the number is a palindrome.

2. Using Mathematical Operations

Another method involves using mathematical operations to reverse the digits of the number and then comparing the reversed number to the original number.

2.2 Program

Kotlin
fun main() {
    val number = 121
    var originalNumber = number
    var reversedNumber = 0

    while (originalNumber != 0) {
        val digit = originalNumber % 10
        reversedNumber = reversedNumber * 10 + digit
        originalNumber /= 10
    }

    if (number == reversedNumber) {
        println("$number is a palindrome.")
    } else {
        println("$number is not a palindrome.")
    }
}

2.3 Output

Kotlin
121 is a palindrome.

In this example, the digits of the number are reversed using a while loop. The reversed number is then compared to the original number to check if it is a palindrome.

3. Using Recursion

Recursion can also be used to check if a number is a palindrome by comparing the first and last digits of the number recursively.

3.2 Program

Kotlin
fun isPalindrome(number: Int): Boolean {
    val originalString = number.toString()
    return isPalindromeHelper(originalString, 0, originalString.length - 1)
}

fun isPalindromeHelper(string: String, start: Int, end: Int): Boolean {
    if (start >= end) {
        return true
    }
    return if (string[start] != string[end]) {
        false
    } else {
        isPalindromeHelper(string, start + 1, end - 1)
    }
}

fun main() {
    val number = 121
    if (isPalindrome(number)) {
        println("$number is a palindrome.")
    } else {
        println("$number is not a palindrome.")
    }
}

3.3 Output

Kotlin
121 is a palindrome.

In this example, the isPalindrome function uses a helper function to recursively compare the first and last characters of the string representation of the number. If the characters match for all recursive calls, the number is a palindrome.

Conclusion

Checking whether a number is a palindrome in Kotlin can be achieved using various methods, each with its own advantages. Converting the number to a string and reversing it provides a simple and direct approach, while using mathematical operations offers an understanding of digit manipulation. Recursion showcases an elegant and concise solution for comparing characters. Depending on the use case and complexity, you can choose the method that best fits your needs.

By mastering these techniques, you’ll be better equipped to handle a variety of problems involving palindromes and string manipulation in your Kotlin projects. Understanding these different approaches enhances your problem-solving skills and broadens your knowledge of Kotlin programming.