Home »
Kotlin »
Kotlin Programs »
Kotlin Basic Programs
Kotlin program to check given number is palindrome number or not
Kotlin | Check Palindrome Number: Here, we are going to learn how to check whether a given number is palindrome or not in Kotlin programming language?
Submitted by IncludeHelp, on April 24, 2020
A palindromic number is a number that remains the same when its digits are reversed. Example: 16461.
Problem statement
Given a number num, we have to check whether num is a palindrome number of not.
Example:
Input:
num = 12321
Output:
12321 is a palindrome number
Input:
num = 12345
Output:
12345 is not a palindrome number
Program to check number is palindrome number or not in Kotlin
/**
* Kotlin Program to check given number is Palindrome number or not
*/
package com.includehelp.basic
import java.util.*
//Function to check Palindrome Number
fun isPalindrome(number: Int): Boolean {
var isPalindromeNumber = false
var sum = 0
var tempNum = number
while (tempNum > 0) {
val r = tempNum % 10
sum = sum * 10 + r
tempNum /= 10
}
if (sum == number) {
isPalindromeNumber = true
}
return isPalindromeNumber
}
//Main Function, Entry Point of Program
fun main(arg: Array<String>) {
val sc = Scanner(System.`in`)
// Input Number
println("Enter Number : ")
val num: Int = sc.nextInt()
//Call Function to check Number
if (isPalindrome(num))
println("$num is a Palindrome Number")
else
println("$num is not a Palindrome Number")
}
Output
Run 1:
Enter Number :
12321
12321 is a Palindrome Number
---
Run 2:
Enter Number :
343
343 is a Palindrome Number
---
Run 3:
Enter Number :
2344
2344 is not a Palindrome Number