# Check for palindrome numbers

• February 19th, 2010, 09:20 AM
SnooSnoo
Check for palindrome numbers
First of, hi I'm a new user here. Learning my first year of Java.
This is just an exam question which I couldn't solve but am eager to find the answer to.

int num[] = {121, 13, 34, 11, 22, 54};

Using the for loop and while loop
The user is to check for palindrome numbers within the array and output it as follows:
121 is a palindrome number
13 is a not palindrome number
34 is a not palindrome number
11 is a palindrome number
22 is a palindrome number
54 is a not palindrome number

Previous I was taught how to print a given integer in reverse

Quote:

int num = 123456789
while (num > 0) {
System.out.print(num % 10);
num /= 10;
}
This allows me to print out the integer in reverse. But in this question I'm required to somehow save the integer generated from the while loop and compare it to the array.
Here's what I did:
Quote:

int num[] = {121, 13, 34, 11, 22, 54};
String check[] = new String[6];
String temp;
for (int i = 0; i < num.length; i++) {
while (num.[i] > 0) {
temp = Integer.toString(num[i] % 10);
check[i] = check[i] + temp;
num[i] /= 10;
} // end while loop
if (check[i] = Integer.toString(num[i]) {
System.out.println(num[i] + " is a palindrome number")
} else {
System.out.println(num[i] + " is not a palindrome number")
} // end for loop
Seems like using Integer.toString causes all my num[i] to become 0.

P.S. I'm not sure if this is the right section to be asking this. If it's in the wrong section, would a Mod move it to the right one please.
• February 19th, 2010, 02:35 PM
CT&SC
Re: Check for palindrome numbers
I can't really follow your code. Take a look at the following and see if it's similar or different from the way I would do it:

Code :

```ListPalindroms(numbers[1...n] : integer) 1. for i := 1 to n 2. do if IsPalindromic(toString(numbers[i])) then print(numbers[i], " is palindromic.") 3. else then print(numbers[i], " is not palindromic.")   IsPalindromic(word : string) 1. if len(word) < 2 then return true 2. else if word[1] != word[len(word)] then return false 3. else return IsPalindromic(word[2...len(word)-1])```
• February 19th, 2010, 07:33 PM
vivekbansal
Re: Check for palindrome numbers
Here is the code that would find out the palindrome number. You need not take another String array if you don't want to use it outside the loop.

Code :

```int num[] = {121, 13, 34, 11, 22, 54}; String check = ""; String temp="";   for(int i=0; i<num.length;i++){ check=Integer.toString(num[i]); while(num[i]>0){ temp=temp+Integer.toString(num[i]%10); num[i]=num[i]/10; }   if(check.equals(temp)){ System.out.println(num[i] + "is a palindrome number"); }else{ System.out.println(num[i] + "is not a palindrome number"); } temp=""; check=""; }```
• March 4th, 2010, 06:05 AM
SnooSnoo
Re: Check for palindrome numbers
Quote:

Originally Posted by vivekbansal
Here is the code that would find out the palindrome number. You need not take another String array if you don't want to use it outside the loop.

int num[] = {121, 13, 34, 11, 22, 54};
String check = "";
String temp="";

for(int i=0; i<num.length;i++){
check=Integer.toString(num[i]);
while(num[i]>0){
temp=temp+Integer.toString(num[i]%10);
num[i]=num[i]/10;
}

if(check.equals(temp)){
System.out.println(num[i] + "is a palindrome number");
}else{
System.out.println(num[i] + "is not a palindrome number");
}
temp="";
check="";
}

I run the program and end up getting
Quote:

0 is not a palindrome number
0 is not a palindrome number
0 is not a palindrome number
0 is not a palindrome number
0 is not a palindrome number
0 is not a palindrome number
I've no idea why the array values become 0. I added a system.out.println OUTSIDE of the for loop and it successfully prints the value.
Quote:

Originally Posted by CT&SC
I can't really follow your code. Take a look at the following and see if it's similar or different from the way I would do it:

Code :

```ListPalindroms(numbers[1...n] : integer) 1. for i := 1 to n 2. do if IsPalindromic(toString(numbers[i])) then print(numbers[i], " is palindromic.") 3. else then print(numbers[i], " is not palindromic.")   IsPalindromic(word : string) 1. if len(word) < 2 then return true 2. else if word[1] != word[len(word)] then return false 3. else return IsPalindromic(word[2...len(word)-1])```

Completely Missing The Point
• March 4th, 2010, 07:57 AM
jasonm
Re: Check for palindrome numbers
Are you sure that is the output you are getting? I pasted your code in directly, and the output I see is:

Code :

```0 is a palindrome number 0 is not a palindrome number 0 is not a palindrome number 0 is a palindrome number 0 is a palindrome number 0 is not a palindrome number```

So the results are correct, but you are actually modifying the integers, which is why they are printing out as 0. Take another look at your loop:

Code :

```while(num[i]>0){ temp=temp+Integer.toString(num[i]%10); num[i]=num[i]/10; }```
• March 4th, 2010, 08:40 AM
SnooSnoo
Re: Check for palindrome numbers
Quote:

Originally Posted by jasonm
Are you sure that is the output you are getting? I pasted your code in directly, and the output I see is:

Code :

```0 is a palindrome number 0 is not a palindrome number 0 is not a palindrome number 0 is a palindrome number 0 is a palindrome number 0 is not a palindrome number```

So the results are correct, but you are actually modifying the integers, which is why they are printing out as 0. Take another look at your loop:

Code :

```while(num[i]>0){ temp=temp+Integer.toString(num[i]%10); num[i]=num[i]/10; }```

Ah yes I see now. I just have to create another array holding the same values and use that when printing.
I didn't realize the /10 part until you pointed it out. ( ¯_¯;)
The original printReverse didn't require the integers to be used in the printing so I didn't think of it.
Thanks!
• March 4th, 2010, 12:59 PM
JavaPF
Re: Check for palindrome numbers
Like the name SnooSnow lol Reminds me of Futurama
• March 4th, 2010, 06:11 PM
vivekbansal
Re: Check for palindrome numbers
Yes, it was getting reset to 0, I should have used variable check, where I am storing the value to print.

Here is the one which will return the result with values.

Code :

``` int num[] = {121, 13, 34, 11, 22, 54}; String check = ""; String temp="";     for(int i=0; i<num.length;i++){ check=Integer.toString(num[i]); while(num[i]>0){ temp=temp+Integer.toString(num[i]%10); num[i]=num[i]/10; }   if(check.equals(temp)){ System.out.println(check + "is a palindrome number"); }else{ System.out.println(check + "is not a palindrome number"); }   temp=""; check=""; }```