How to find the largest palindrome made from the product of two 2-digit numbers in PHP

1 Answer

0 votes
// Function to check if a number is a palindrome
function isPalindrome($num) {
    $temp = $num;
    $rev = 0;

    while ($temp > 0) {
        $rev = $rev * 10 + ($temp % 10);
        $temp = (int)($temp / 10);
    }

    return $rev === $num;
}

// Function to find the largest palindrome from the product of two 2-digit numbers
function getLargestPalindromeOfTwo2DigitNumbers() {
    $largestPalindrome = 0;

    for ($i = 10; $i < 100; $i++) {
        for ($j = 10; $j < 100; $j++) {
            $product = $i * $j;
            if (isPalindrome($product) && $product > $largestPalindrome) {
                $largestPalindrome = $product;
            }
        }
    }

    return $largestPalindrome;
}


echo "The largest palindrome made from the product of two 2-digit numbers is: " . getLargestPalindromeOfTwo2DigitNumbers() . "\n";



/*
run:

The largest palindrome made from the product of two 2-digit numbers is: 9009

*/

 



answered May 11, 2025 by avibootz
...