Java Program for Palindrome Number

 


In this post, we will learn, How to write a program to display Palindrome Number on given user input.

Before moving to the programming part, Let's try to understand, What is Palindrome Number? 


Palindrome Number:- It is a number that reads the same from last to first (backward) and first to last (forward)

Ex- 121, 333, 131, 1221, 12321 ..... etc.


Code-


public class PalindromeNumber{

    public static void main(String[] args) {

        int num = 121, reversedInteger = 0, remainder, originalInteger;

        originalInteger = num;

        // reversed integer is stored in variable 

        while( num != 0 )

        {

            remainder = num % 10;

            reversedInteger = reversedInteger * 10 + remainder;

            num  /= 10;

        }

        // palindrome if orignalInteger and reversedInteger are equal

        if (originalInteger == reversedInteger)

            System.out.println(originalInteger + " is a palindrome number.");

        else

            System.out.println(originalInteger + " is not a palindrome number.");

    }

}


Output:-

121 is a palindrome number.


Explanation:- 

  • This program takes an integer as input and firstly assign it to originalInteger variable. 
  • Then using while loop we reverse the integer and store in reversedInteger variable.
  • Then we compare the original integer with reversed integer
  • if both are same then the number is a palindrome number.

Code using Recursive Way


public class PalindromeRecursive {


    public static boolean isPalindrome(int num) {

        if (num < 0) 

            num = -num;

        if (num < 10) 

            return true;

        if (num % 10 != num / (int) Math.pow(10, (int) Math.log10(num))) 

            return false;

        return isPalindrome(num % 10 + (num / 10));

    }

    public static void main(String[] args) {

        int num = 121;

        if (isPalindrome(num)) {

            System.out.println(num + " is a palindrome number.");

        } else {

            System.out.println(num + " is not a palindrome number.");

        }

    }

}


Output:- 

121 is a palindrome number.


Explanation:-


In this program defines a method "isPalindrome(int num)" which takes an integer as input and checks if it is a palindrome using recursion function. This method first checks if the input number is negative, in which case it makes it positive. Then it checks if the number is less than 10, in which case it returns true as all single-digit numbers are palindrome.


Then it uses the modulus operator "%" and the division operator "/" to check if the first and last digits of the number are equal. If they are not equal, it returns false, indicating that the number is not a palindrome. If they are equal, it calls the `isPalindrome` method recursively with the number obtained by removing the first and last digits of the original number.


The main method then calls the isPalindrome method and checks its return value to determine if the input number is a palindrome or not.


Hope this post will helpful. Please put your valuable comment and share them with your friends............

1 Comments

  1. public static boolean isPalindrome(int num) {

    if (num < 0)

    num = -num;

    if (num < 10)

    return true;

    if (num % 10 != num / (int) Math.pow(10, (int) Math.log10(num)))

    return false;

    return isPalindrome(num % 10 + (num / 10));

    }

    public static void main(String[] args) {

    int num = 121;

    if (isPalindrome(num)) {

    System.out.println(num + " is a palindrome number.");

    } else {

    System.out.println(num + " is not a palindrome number.");

    }

    }

    }



    Here output is displaying wrong as "121 is not a palindrome number."

    ReplyDelete

Post a Comment

Previous Post Next Post