Q:

Kotlin program to print all palindromes in a given range

belongs to collection: Kotlin Basic Programs

0

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

Given a range from start to end, we have to check and print all palindromes numbers between start and end (includes start and end).

Example:

    Input:
    start = 1
    end = 100

    Output:
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 
    22, 33, 44, 55, 66, 77, 88, 99]

All Answers

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

Program to print all palindromes in a given range in Kotlin

/**
 * Kotlin Program to check find out Palindrome number 
 * between given Range(include START and END)
*/

package com.includehelp.basic

import java.util.*


//Function to check Palindrome Number
fun findPalindrome(number: Long): Boolean {
    var isPalindromeNumber = false
    var sum : Long= 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>) {
    //Input Stream
    val sc = Scanner(System.`in`)

    //Input Start of Range
    print("Enter Start of Range  : ")
    val start: Long = sc.nextLong()

    //Input End of Range
    print("Enter End of Range  : ")
    val end: Long = sc.nextLong()

    //Declare Mutable List to hold Palindromes
    val list: MutableList<Long> = ArrayList()

    //iterate through loop start to end to find Palindrome in Range
    for (i in start..end) {
        if (findPalindrome(i)) {
            list.add(i)
        }
    }

    println("Palindrome Numbers from $start to $end  : $list")
}

Output

RUN 1:
Enter Start of Range  : 1
Enter End of Range  : 100
Palindrome Numbers from 1 to 100  : [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 
22, 33, 44, 55, 66, 77, 88, 99]
---
RUN 2:
Enter Start of Range  : 999
Enter End of Range  : 9999
Palindrome Numbers from 999 to 9999  : [999, 1001, 1111, 1221, 1331, 1441, 1551, 
1661, 1771, 1881, 1991, 2002, 2112, 2222, 2332, 2442, 2552, 2662, 2772, 2882, 
2992, 3003, 3113, 3223, 3333, 3443, 3553, 3663, 3773, 3883, 3993, 4004, 4114, 
4224, 4334, 4444, 4554, 4664, 4774, 4884, 4994, 5005, 5115, 5225, 5335, 5445, 
5555, 5665, 5775, 5885, 5995, 6006, 6116, 6226, 6336, 6446, 6556, 6666, 6776, 
6886, 6996, 7007, 7117, 7227, 7337, 7447, 7557, 7667, 7777, 7887, 7997, 8008, 
8118, 8228, 8338, 8448, 8558, 8668, 8778, 8888, 8998, 9009, 9119, 9229, 9339, 
9449, 9559, 9669, 9779, 9889, 9999]

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 check whether a number is prime ... >>
<< Kotlin program to check given number is palindrome...