-1

Following is my code which adds products of all 3 digit numbers into an array List. Then prints those numbers which are palindrome. I am getting 121 as output. Why not other palindrome number??

import java.util.ArrayList;

public class eular {
    int reverse=0;


    public boolean palindrome( Integer num){
        int remainder=0;
        int n=num;
        while(num!=0){
            remainder = num % 10;
            reverse = reverse * 10 + remainder;
            num = num / 10;
                    }
        if(n==reverse)
            return true;
        else
        {
            return false;
        }

    }

    public static void main(String args[]){
        eular e=new eular();
        ArrayList<Integer> a=new ArrayList<Integer>();
    for(int i=100;i<=999;i++)
        for(int j=100;j<=999;j++){

        a.add(i*j);

        }
        for (Integer integer : a) {

    if(e.palindrome(integer)){
        System.out.println(integer);;
    }

        }

    }

    }
6
  • Because in your arraylist only 121 is a palindrome and hence the value is printed...
    – CoderNeji
    Jul 30, 2015 at 12:06
  • 1
    Have you tried to debug your code to see what is happend? I am not the downvoter
    – Jens
    Jul 30, 2015 at 12:07
  • But there are others too like 9009,etc Where are other numbers? Jul 30, 2015 at 12:07
  • 2
    How come you get 121 as output when it can't be in the list at all? Your program does not return any output when I run it. Jul 30, 2015 at 12:09
  • Also your code requires some modification
    – CoderNeji
    Jul 30, 2015 at 12:10

2 Answers 2

7

Your reverse variable should be initialized every time you call a palindrome method. Move the line int reverse=0; inside that method.

3

First, you've forgot to declare reverse (edit: or did it in the wrong context as a field of euler class: in that case you have to assign reverse to zero before each call of the palindrome):

   // static: you don't want "this"
   public static boolean palindrome(Integer num) { 
     int n = num; 
     int reverse = 0; // <- this was omitted (or misplaced as a field of euler)

     while (num != 0) {
       reverse = reverse * 10 + num % 10;
       num = num / 10;
     } 

     // ifs can be so ugly...
     return n == reverse;
   }

Second, you don't want that huge arrays:

   ...
   for (int i = 100; i <= 999; i++)
     for (int j = 100; j <= 999; j++) {
       int item = i * j;

       if (palindrome(item))
         System.out.println(item);
     }
3
  • 1
    No, he didn't forget, he just declared it as a variable of euler and not a local variable in the method. And list of 810000 Integers shouldn't be critical. Of course, not required, but also not really critical. Jul 30, 2015 at 12:14
  • @Florian Schaetz: in this case it's a plausible cause of the error: reverse should be assigned to zero before each call of palindrome Jul 30, 2015 at 12:15
  • 1
    Yes, it should, but that does not mean "just" adding reverse, but moving it (from the class into the method). This was not "omitted", but "misplaced". Jul 30, 2015 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.