Search code examples
javapalindrome

Why this is not a palindrome?


I'm trying to find largest palindrome from product of 2 3-digit number (100-999). My approach was to check is it a palindrome or not using String. I am getting small values.

import acm.program.*;

 public class Palindrom extends ConsoleProgram{

    public void run(){
        int last = 0;
        for (int i = 100; i <= 999; i ++){
            for (int k = 100; k <= 999; k++){
                int p = i*k;
                String str = "" + p;
                if(isPalindorme(p, str)){
                    last = p;

                }
            }
        }
        println(last);
    }

    public boolean isPalindorme(int p, String str){
        for (int i = 0; i < str.length()/2; i++){
            if (str.charAt(i) != str.charAt(str.length()-1-i)){
                return false;
            }
        }
        return true;    
    }

 }

I think that maybe the mistake is at this line:

String str = "" + p;

Solution

  • You need to also check that p > last.