Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Check for palindrome numbers

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

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:
int num[] = {121, 13, 34, 11, 22, 54};
String check[] = new String;
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.  Reply With Quote

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

```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 != word[len(word)] then return false
3. else return IsPalindromic(word[2...len(word)-1])```  Reply With Quote

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

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

5. ## Re: Check for palindrome numbers 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
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. 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:

```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 != word[len(word)] then return false
3. else return IsPalindromic(word[2...len(word)-1])```
Completely Missing The Point  Reply With Quote

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

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

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

7. ## Re: Check for palindrome numbers 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:

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

```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!  Reply With Quote

8. ## Re: Check for palindrome numbers

Like the name SnooSnow lol Reminds me of Futurama  Reply With Quote

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

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