In my previous post I discussed ways to detect string palindromes. In this post I will discuss an efficient method to test for integer palindrome. Please note that the method outlined in my earlier post will work perfectly if integers are stored as string. Strings can hold integers just like it can store other printable characters but it’s always better to use appropriate data types depending on the requirement. Integers can be used for calculations unlike strings and are faster.

## Reversing an integer

In order to test a positive integer for palindrome, we will first reverse the integer and then compare the reversed integer with the original integer. A Java function to reverse a positive integer argument is as follows:

public static int reverse(int n){ int reverse=0; for( int temp=n; temp > 0; temp /= 10 ){ reverse = reverse*10 +temp%10; } return reverse; }

In the above problem the variable reversed has been initialized to 0. The *for* loop would start with the control variable temp initialized to the value of n. A digit can only be extracted from the back of an integer using numerical methods. To extract a digit from the integer stored in the variable temp, we will determine the value of temp % 10. Students must understand that the remainder of any number divided by 10 gives the last digit of the number. This works even if the number is a single digit number, even for 0. Once we have extracted the digit from the integer we would remove that digit by dividing the integer by 10. Since both the dividend and the divisor are integers, the quotient would also be an integer. For example, 123/10 would give 12 and not 12.3. thus to remove the last digit of an integer we would divide it by 10. We will add this extracted digit to the variable reverse after multiplying it by 10. The change in value for various variables for n = 1234 will be as follows:

temp | digit ( temp % 10 ) | reverse |

1234 | 4 | 4 |

123 | 3 | 43 |

12 | 2 | 432 |

1 | 1 | 4321 |

0 | Loop over |

The reverse function can also be implemented using the* while* loop as follows:

public static int reverse(int n){ int reverse=0; int temp=n; while( temp > 0 ){ reverse = reverse*10 +temp%10; temp /= 10 } return reverse; }

## Testing for integer palindrome

The complete program to test for integer palindrome is as follows:

class IntegerPalindrome{ public static int reverse(int n){ int reverse=0; for( int temp=n; temp > 0; temp /= 10 ){ reverse = reverse*10 +temp%10; } return reverse; } public static boolean isPalindrome(int n){ return n==reverse(n); } public static void main(String args[]){ System.out.println(isPalindrome(12321)); System.out.println(isPalindrome(1)); System.out.println(isPalindrome(123)); } }

One more function isPalindrome() returning a boolean value has been introduced in the above program to test for integer palindrome. The function isPalindrome would call the reverse() function with n as argument and then compare the returned value with the value of n. There is no need for an *if* statement inside the isPalindrome() function because the expression n==reverse(n) is a boolean expression in resulting in either true or false. Testing this boolean result again against a true or false constant is redundant and useless.

As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.

### Vinay Singh

#### Latest posts by Vinay Singh (see all)

- Sorting non boundary elements - February 19, 2016
- Circular Prime – Solution of ISC Computer Practical 2016 - February 18, 2016
- Array Pattern - February 15, 2016

Pingback: Automorphic number | www.VinaySingh.info