# How to know if an integer is a palindrome?

• 10-18-2011, 02:42 AM
akeni
How to know if an integer is a palindrome?
hi guys. I'm stuck on some coding.

Without using strings and array, i am trying to reverse the number so i can test if it is a palindrome number( which means when an integer is in reversed order, it is equal to the original order)

what i have so far:

Code:

```public static int getPal(int num){                 int i;         int digit;         int rem;         double sum=0;         double length=0;         double n;                 length= findLength(num);                 for (i=0;i<num; i++){                                 if (i<10){                         System.out.println(""+i);                 }                                                 }                         while (num>10){                 n=num;                 while (n>0){                 rem=x%10;                 sum=rem*length+sum;                 length= length/10;                 n=n/10;                 }                                 if (num ==sum) {                         System.out.println(""+n);                                                 }                                 num--;                                 } return 0; } public static double findLength(int num){         double length=0;                 while (num>0){                 num= num/10;                 length++;         }         length= Math.pow(10,length-1);         return length; }```
• 10-18-2011, 02:50 AM
pbrockway2
Re: How to know if an integer is a palindrome?
Does the program do what you expect? If not describe both the expected and actual behaviour when you give it some specific input. If it does not compile post the exact and entire compiler message and say which line(s) of your code it is referring to.

-----

Perhaps you could also describe *how* you are deciding whether the input is a palindrome. In particular the println() statements seem a little mysterious. It is important to have a (comprehensive and specific) plan before rushing into code.
• 10-18-2011, 03:12 AM
akeni
Re: How to know if an integer is a palindrome?
Okay, i fixed it. The problem was the while loop and me using doubles instead of ints.

Code:

``` public static int getPal(int num){                 int i;         int digit;         int rem;         int x;         int sum;         int length=0;         int n;                 System.out.println("");                 for (i=0;i<num; i++){                                 if (i<10){                         System.out.println(""+i);                 }                                                 }                         while (num>10){                 n=num;                 sum=0;                 length= findLength(num);                 while (n>0){                 rem=n%10;                 sum=rem*length+sum;                 length= length/10;                 n=n/10;                                         if (num ==sum) {                                 System.out.println(""+sum);                                                         }                 }                                 num--;                         } return 0; } public static int findLength(int num){         double length=0;         int i=1;                 while (num>10){                 num= num/10;                 length++;         }                         while (length>0){                 i = i*10;                 length--;         }         return i; }```