tags:

views:

186

answers:

7

I have the following code and i want to print the palindrome on console. Please let me know what should come inside the if condition so that it can print all palindrome in between 0 to 10000.

The palindrome is 161, 1221, 4554, etc....

Java code:

int palindrome[];
palindrome = new int[10000];

for (int count = 0; count < palindrome.length; count++ ){
    palindrome[count] = 0;
    if(){
        System.out.println(count);
    }
}
+6  A: 

This will do:

    if (isPalindrome(count)) {
        System.out.println(count);
    }

...

public boolean isPalindrome(int num) {
    // implement method here (ie: do your homework)
}
NullUserException
Do i have to include any library to call this function?
fawad
@fawad: no, you don't.
Bart Kiers
Actually, i'm not very good in Java that is why i wanted to know the logic or syntax for that.
fawad
@fawad `Integer.toString()` and `String.charAt()` are all you need to implement it.
NullUserException
oh thank you so much. Secondly, please let me know which editor can i use for JAVA. That is easily downloadable and compile the JAVA code.
fawad
+1 for a good answer (and also because "NullUserException" is an awesome name!)
Joviee
+2  A: 

You need to transform the number into a string and then check if the string reversed is equal to the original string. This looks a lot like some homework, if so please add the appropriate tag.

RC
please add the appropriate tag ... What does that mean?
fawad
someone did it for you question like yours are tagged "homework"
RC
oh but this is not the homework infact. Anywaz, i don't know the syntax of java very well but i'm very good in C/C++ that is why i was taking such help.
fawad
@fawad If you follow aniri suggestion, the could would look identical in C and Java (well, NEARLY identical ;-)).
Helper Method
Btw, this solution is way faster than reversing a StringBuilder.
Helper Method
+1  A: 

You could also reverse the number and check if the reversed one and your number are equal.

To reverse the number: keep dividing it with 10 until the result is 0. Save the remainders of the divisions. Starting from the first remainder you get, multiply it by 10 and add the next remainder. Keep multiplying by 10 and adding remainders till you use all the remainders.

aniri
I think that would be easy if i change the integer to string and then reverse the string and match with the original one.Thanks, your logic is also very appropriate
fawad
+1  A: 

Something like this might work

public boolean isPalindrome(final int num) {
    final String s = Integer.toString(num);
    return s.equals(new StringBuffer(s).reverse().toString());
}
jvdneste
I think this can work very well. I'll test it.Thanks alot.
fawad
A: 

To revers I use a StringBuffer, StringBuffer(num).reverse().toString();

Kanzeon
+1  A: 

The code golf solution:

   public static boolean isPalindrome(int number) {
       return ("" + number).equals(new StringBuilder("" + number).reverse().toString());
   }
Landei
A: 

Here are some hints:

a) To get the last digit of a number use the remainder (%) operator

Example:
result = number % 10; // 21 % 10 = 1

b) To cut of the last digit, divide by 10

Example:
number = number / 10; // 21 / 10 = 2;

c) Multiply the result by 10

Example:
result = result * 10; // 1 * 10 = 10

d) Repeat until some condition is fulfilled (shouldn't be too hard to figure out ;-))

Helper Method