Palindrome number program in Java
Write java program to check if a number is palindrome in Java?
A number that does not change on reversing is called a palindrome number. In other words, when reversing the digits of a number does not affect the number is called the palindrome number. For example, 121, 343, 555, 606, 999, 1001, 1331 are the palindrome numbers. In this section, we will create a Java program to check the number is palindrome or not.
Algorithm to Find Palindrome Number
- Get the number, which is going to be checked for palindrome.
- Create a copy of the number by storing it in some temporary variable.
- Now, reverse the number.
- Check with the copy whether the reversed number is equal or not.
- If it is equal, then we have a palindrome number.
- Otherwise, the number is not a palindrome.
The following Java program demonstrates how to check a given number is a palindrome number or not by using a while loop.
Iterative Approach
Filename: PalindromeNumberExample.java
public class PalindromeNumberExample { public static void main(String[] args) { int no = 535; // To be checked for palindorme int tempVar = no; // Creating the copy of ‘no’ int sum = 0; while(no > 0) { // reversing the number sum = sum * 10; sum = sum + (no % 10); no = no / 10; } if(tempVar == sum) System.out.print("The number " + tempVar +" is a palindrome number."); else System.out.print("The number " + tempVar +" is not a palindrome number."); } }
Output:
The number 535 is a palindrome number.
Explanation: We have used a Java while loop to reverse the number. The number of iterations of the while loop depends on the number of digits present in the given number. In our case, it is 3. Let’s see the working of the program.
First Iteration: sum = sum * 10. However, we have initialized the value of sum as 0; Therefore, we get sum = 0 * 10 = 0. Again, we update the value of sum by adding the remainder we get by dividing the given number (535). Thus, we get sum = 0 + (535 % 10). sum = 0 + 5 = 5. Finally, we divide the given number by 10 and assign the quotient to the number. no = 535 / 10 = 53. The updated value of no variable is 53 which is to be used in the next iteration.
Second Iteration: In the second iteration, the value of sum is 5. Hence, sum = 5 * 10 = 50. Again, we add the remainder. This time the number is 53. Hence, we have sum = 50 + (53 % 10). sum becomes 50 + 3 = 53. At last, the given number shrink again by one digit and becomes 5. no = 53 / 10 = 5.
Third Iteration: The process is similar. The sum is 53. Therefore, sum = 53 * 10 = 530. After adding the remainder, we get 535. Also, the number becomes no = 5/10 = 0. Since every time we have checked the while loop condition. When the condition no>0 returns false, the loop terminates.
Recursive Approach
We can also check the palindrome number by using the recursive approach.
Filename: PalindromeNumberExample1.java
public class PalindromeNumberExample1 { static boolean isPalindrome(int no, int tempVar, int sum) { if(no < 0) return false; if(no == 0) return sum == tempVar; return isPalindrome(no/10, tempVar, sum * 10 + (no%10)); } public static void main(String[] args) { int a[] = {4, 434, 909, 100, -66, 123, 797}; for(int i = 0; i < a.length; i++) if(isPalindrome(a[i], a[i], 0)) System.out.println("The number " + a[i] +" is a palindrome number."); else System.out.println("The number " + a[i] +" is not a palindrome number."); } }
Output:
The number 4 is a palindrome number. The number 434 is a palindrome number. The number 909 is a palindrome number. The number 100 is not a palindrome number. The number -66 is not a palindrome number. The number 123 is not a palindrome number. The number 797 is a palindrome number.
Explanation: In the recursive approach also, we have created a copy of the number and then compared it with the number that we have reversed. Note that as per mathematical definition, a negative number can never be a palindromic number.