Java Program to Check Number is Palindrome.
Java Program to Check Number is Palindrome. Number is called Palindrome, if the reverse of number is equal to original number. Example: 12321, 545.
In this post, we will see Algorithm to check whether number is palindrome or not.
Java Program to check whether Number is Palindrome or not.
It is very easy to check whether Number is Palindrome or not.
Approach 1:
In this approach,
STEP 1: Reverse the original number.
STEP 2: Check, whether original number and reversed number is same. If Yes, then number is
Palindrome otherwise not
Lets understand above algorithm step by step with below example.
Java Program to Check number is Palindrome or not.
package com.javabypatel.string; public class PalindromeCheck { public static void main(String[] args) { palindromCheck(12321); } private static void palindromCheck(int number){ if(number < 0){ System.out.println("Invalid number"); return; } int temp = number; int reverseNumber = 0; while(number > 0){ int mod = number % 10; //Get last digit of number reverseNumber = (reverseNumber * 10) + mod; //Append Last digit got to reverseNumber. number = number/10; //Get the remaining number except last digit. } if(temp == reverseNumber){ System.out.println("Number is Palindrome"); }else{ System.out.println("Number is not Palindrome"); } } }
Approach 2:
STEP 1: Convert the Number to String by using String.valueOf() method.
STEP 2: Reverse the String.
STEP 3: Compare Reversed String with String we got in STEP 1, if both are same then Number is
Palindrome else not.
package com.javabypatel.string; public class PalindromeCheck{ public static void main(String args[]){ System.out.println(isPalindrome(12122)); } public static boolean isPalindrome(int number){ if(number < 0){ System.out.println("Invalid number"); return false; } String originalString = String.valueOf(number); String reversedString = ""; for (int i = originalString.length()-1; i >= 0; i--) { reversedString += originalString.charAt(i); } return originalString.equals(reversedString); } }
Approach 3:
In Approach 2, we reverse the whole String and then compared it with original string.
In Approach 3, we will check whether string is palindrome without reversing original string and by comparing the characters of original string from both end that is from front and back together.
STEP: 1
Take 2 variable, pointer1 and pointer2.
pointer1 initialise to index 0 and pointer2 initialise to originalString.length()-1.
STEP 2:
Compare characters at pointer1 and pointer2, if they are not same, then they are not Palindrome and stop. If they are same then increment pointer1, decrement pointer2.
STEP 3: Repeat STEP 2 til pointer1 < pointer2.
package com.javabypatel.string; public class PalindromeCheck{ public static void main(String args[]){ System.out.println(isPalindrome(1221)); } public static boolean isPalindrome(int number){ if(number < 0){ System.out.println("Invalid number"); return false; } String originalString = String.valueOf(number); int pointer1 = 0; int pointer2 = originalString.length()-1; while(pointer1 < pointer2) { if(originalString.charAt(pointer1) != originalString.charAt(pointer2)) { return false; } pointer1++; pointer2--; } return true; } }
I hope below diagram wil help you understand algorithm in better way.
Approach 4:
In this approach, we compare first and last digit of a number, (example say 12321, so we compare first 1 and last 1).
- If they are not same, return false,
- If they are same, remove first and last digit from the number(remaining number 232 ) and repeat the same steps again for remaining number.
package com.javabypatel.string; public class PalindromeCheck{ public static void main(String[] args) { int number = 12321; boolean result = checkPalindrome(number); System.out.println(result); } private static boolean checkPalindrome(int number) { int divisor = findDivisor(number); while (number != 0) { int lastDigit = number % 10; //for 12321 % 10 -> last 1 int firstDigit = number / divisor; //for 12321 / 10000 -> first 1 if (firstDigit != lastDigit) { return false; } number = number % divisor; //for 12321 % 10000 -> remove first 1 from number = 2321 number = number / 10; //for 2321/10 -> remove last 1 from number = 232 //we remove two numbers from 12321 and new number is 232, so we need to calculate new divisor //we remove two numbers so two zeros should be removed from divisor, so we can do divisor/100 to get //new divisor divisor = divisor / 100; //repeat the step for 232 } return true; } private static int findDivisor(int number){ int divisor = 1; // 12321/1 = 12321(it is >= 10), so 12321/10 = 1232, so 12321/100 = 123, so 12321/1000 = 12, so 12321/10000 = 1 end while (number / divisor >= 10) { divisor = divisor * 10; } return divisor; } }
You may also like to see
Check whether String is Palindrome or Not in Java.
Count number of Bits to be flipped to convert A to B
Count number of Set Bits in Integer.
Check if number is Power of Two.
Find all subsets of a set (Power Set).
Skyline Problem in Java.
Find Minimum length Unsorted Subarray, Sorting which makes the complete array sorted
Count trailing zeros in factorial of a number
When to use SOAP over REST Web Service. Is REST better than SOAP?
Enjoy !!!!
If you find any issue in post or face any error while implementing, Please comment.
Post a Comment