Palindrome Number - Java Program
Palindrome Number is a number which remains the same on revering the digits in the number. To check if a number is palindrome, A new number is formed by finding each digit from the last digit. Each digit is extracted by finding the remainder when divided by 10.
Algorithm
Step 1: Initialize the reverse number as zero. | reverse = 0 |
Step 2: Consider the number which you want to find whether it is a palindrome. Store it in a temporary variable temp. | temp= num |
Step 3: Find the remainder after dividing the temp by 10. | remainder = temp % 10 |
Step 4: Multiply reverse number by the remainder found in the previous step. | reverse=(reverse*10) + remainder |
Step 5: Subtract the remainder from temp. | temp = temp - remainder |
Step 6: Store the quotient after dividing the temp by 10. | temp = temp / 10 |
Step 7: Repeat the steps 3 to 6 until tenp becomes zero. | while(temp>0) |
Step 8: if Number is equal to reverse, then the number is a palindrome number. | if(num==reverse) |
Step 8: if Number is not equal to reverse, then the number is NOT a palindrome number. | else |
Java Program
public class PalindromeNumber {
public static void main (String args[]) {
int num = 12321;
int reverse = 0;
int temp= num;
while(temp>0) {
int remainder = temp % 10;
reverse=(reverse*10) + remainder;
temp = temp - remainder;
temp = temp / 10;
}
if(num==reverse) {
System.out.println("The Number " + num + " is a palindrome");
}
else {
System.out.println("The Number " + num + " is not a palindrome");
}
}
}