Problem 4. Largest palindrome product

public class euler4 {
    public static void main(String[]args) {
        int n1 = 999;
        int n2 = 999;
        int max = 0;
        int number = 0;
        while (n1 > 99) {
            while (n2 > 99) {
                if (isPalindrome(n1 * n2)) {
                    System.out.println(n1*n2 + " " + n1 + " " + n2);
                    number = n1*n2;
               	    if (max < number)
               	        max = number;
               	    break;
                }
                else {
               	    n2--;
                }
            }
            n1--;
            n2 = n1;
        }
        System.out.println(max);
    }
    public static boolean isPalindrome(int n) {
        String s = new Integer(n).toString(); 
        int len = s.length();
        int i = 0;
        int j = len-1;
        while (i < j) {
            if (s.charAt(i++) != s.charAt(j--))
                return false;
        }
        return true;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>