How to check Number is Palindrome number or not using Java

Number is called Palindrome number if original number and reverse of original number are same.
ex. 46364. Just reverse the number and compare it with original number. that's it

Java Program to check number is palindrome or not :

package com.anuj.algorithms;

/**
 *
 * @author Anuj Patel
 */
public class Palindrom {

    protected void checkPalindrom(int num) {
        int reverse = 0;
        int original = num;

        for (int i = 0; i <= num; i++) {
            int rand = num % 10;
            num = num / 10;
            reverse = reverse * 10 + rand;
        }
        System.out.println("Original Number is - " + original);
        System.out.println("Reverse Number is - " + reverse);

        if (reverse == original) {
            System.out.println("number is Palindrom\n");
        } else {
            System.out.println("number is not palindrom\n");
        }
    }

    public static void main(String[] args) {
        Palindrom palindrom = new Palindrom();
        //palindrom.checkPalindrom(46361);
        palindrom.checkPalindrom(46364);
    }
}

Output :
Original Number is - 46361
Reverse Number is - 16364
number is not palindrom

Original Number is - 46364
Reverse Number is - 46364
number is Palindrom

No comments:

Post a Comment