Q:

Kotlin program to check given number is palindrome number or not

belongs to collection: Kotlin Basic Programs

0

palindromic number is a number that remains the same when its digits are reversed. Example: 16461.

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Kotlin program to print all palindromes in a given... >>
<< Kotlin program to display Armstrong numbers betwee...