# Thread: How to know if an integer is a palindrome?

1. Member Join Date
Oct 2011
Posts
31
Rep Power
0

## 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:

Java 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;
}```  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19

## 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.  Reply With Quote

3. Member Join Date
Oct 2011
Posts
31
Rep Power
0

## 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.

Java 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;
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•