Recursive

Palindromic number

Pinterest LinkedIn Tumblr

A palindromic number is a number that remains the same when its digits are reversed.

Write a program to check palindrome.Example:-
55, 66, 77, 88, 99 are Palindrome numbers
23, 45, 90 not palindrome numbers

source Code

package com.dsacode.Probelms;
 
public class Palindrome {
     
    private int reverse(int number){
        int res  = 0;
         
        while(number > 0){
            res = res * 10 + number %10;
            number = number /10;
        }
        return res;
    }
     
    public boolean isPalindrome(int num){
         
        if(num == reverse(num))
            return true;
         
        return false;
    }
     
    public static void main(String[] args) {
         
        Palindrome p = new Palindrome();
         
        System.out.println("Check 11 is polindrome:"+ p.isPalindrome(11));
         
        System.out.println("Check 100 is polindrome:"+p.isPalindrome(100));
 
    }
 
} 

Output

Check 11 is polindrome:true
Check 100 is polindrome:false

Algorithm Explanation

Read the number and send to reverse the number.
Compare reverse return with original number
If both are same, palindrome number

Reference

  1. http://en.wikipedia.org/wiki/Palindromic_number
  2. http://mathworld.wolfram.com/PalindromicNumber.html
  3. https://www.mathsisfun.com/definitions/palindromic-numbers.html

Write A Comment