A palindrome is a number that remains the same when the digits of a number are reversed. Palindrome numbers are 121 and 14541, for example. The reversed number is the same as the given number if the digits of the palindrome numbers are reversed. Palindrome numbers are those listed below that remain the same when written in the opposite order. In this post, we’ll look at how to write a Java programme that checks whether a given number is a palindrome.
Palindrome Number
A number is referred to as a palindrome if it is equal to the reverse of that same number. Palindrome is a number when the digits of a number are reversed, the number remains the same. Palindrome numbers are symmetry across a vertical axis.
Palindrome numbers are those listed below that remain the same when written in the opposite order.
131, 45654, 7496947
The numbers in the list below are not palindromes. A different number will appear on the reverse.
1310, 03430, 2345
The following is the logic for determining the palindrome number. Take the given number and save it to a local variable. Using the divide and mod operators, reverse the given number. The reversed number is saved in a programme local variable. Compare the reversed and given numbers. If both are the same, print the given number as a palindrome. Otherwise, the provided number is not a palindrome.
The programme in the Java example below determines whether the given number is a palindrome or not.
package in.yawin; public class Palindrome { public static void main(String[] args) { int value = 12321; int temp = value; int reverseValue = 0; int remainder = 0; while (temp != 0) { remainder = temp % 10; reverseValue = reverseValue * 10 + remainder; temp = temp / 10; } if (value == reverseValue) { System.out.println(value + " is a Palindrome number."); } else { System.out.println(value + " is not a Palindrome number."); } } }
Output
12321 is a Palindrome.
Explanation
The java programme will determine whether or not the given number is a palindrome. Another variable is used to store the given number. Assume the variable’s name is temp. Divide the given number by ten to find the remainder. The digit in the zeroth place is the remainder number. The remainder is obtained by dividing the given number by ten. If you apply this logic in the loop and append the remainder by multiplying the number by 10, you will get the inverse of the given number. If the reverse and given numbers are the same, the given number is a palindrome.