How to find the largest palindrome made from the product of two 3-digit numbers in Java

1 Answer

0 votes
public class LargestPalindrome {

    public static boolean isPalindrome(int number) {
        String str = Integer.toString(number);
        String reversed = new StringBuilder(str).reverse().toString();
        return str.equals(reversed);
    }

    public static int largestPalindrome() {
        int maxPalindrome = 0;

        for (int i = 999; i >= 100; i--) {
            for (int j = i; j >= 100; j--) { // Avoid duplicate calculations
                int product = i * j;
                if (product > maxPalindrome && isPalindrome(product)) {
                    maxPalindrome = product;
                }
            }
        }

        return maxPalindrome;
    }

    public static void main(String[] args) {
        System.out.println("Largest palindrome: " + largestPalindrome());
    }
}



/*
run:

Largest palindrome: 906609

*/

 



answered Jul 29, 2025 by avibootz
...