A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Palindromic Number in Kotlin page! Here, you’ll find the source code for this program as well as a description of how the program works.
fun main(args: Array<String>) {
var n: Int
var num: Int
var digit: Int
var rev: Int = 0
try {
num = args[0].toInt()
if (num >= 0){
n = num
do {
digit = num % 10
rev = (rev * 10) + digit
num = num / 10
}while (num != 0)
if (n == rev){
println(true)
}else{
println(false)
}
}else{
println("Usage: please input a non-negative integer")
}
}catch(e: Exception){
println("Usage: please input a non-negative integer")
}
}
Palindromic Number in Kotlin was written by:
If you see anything you’d like to change or update, please consider contributing.
Note: The solution shown above is the current solution in the Sample Programs repository as of Oct 11 2022 01:31:51. The solution was first committed on Oct 08 2020 16:44:11. As a result, documentation below may be outdated.
code for palindrome.kt:
fun main() {
var n: Int
var num: Int
var digit: Int
var rev: Int = 0
print("Enter a positive number: ")
num = readLine()!!.toInt()
n = num
do {
digit = num % 10
rev = (rev * 10) + digit
num = num / 10
}while (num != 0)
if (n == rev){
println(true)
}else{
println(false)
}
}
To run the solution to check if a number is a palindrome or not, we first create some variables we will use in the algorithm in the main function as shown here:
var n: Int
var num: Int
var digit: Int
var rev: Int = 0
Then we will prompt the user to input the number that needs to be checked:
print("Enter a positive number: ")
num = readLine()!!.toInt()
Then we use a do while loop to reverse the number that was typed in:
do {
digit = num % 10
rev = (rev * 10) + digit
num = num / 10
}while (num != 0)
Finally, we check if the number and its reverse are the same or not. If they are the same, we tell the user that it’s true, otherwise we tell the user that it’s false, as shown here:
if (n == rev){
println(true)
}else{
println(false)
}