// Javascript problem to find out the
// largest palindrome number
// which is product of two
// n digit numbers.
// Function to calculate largest
// palindrome which isproduct of
// three n-digits numbers
function larrgestPalindrome(n)
{
let upper_limit = Math.pow(10, n) - 1;
console.log("upper_limit: " + upper_limit)
// largest number of n-1 digit.
// One plus this number
// is lower limit which is
// product of two numbers.
let lower_limit = 1 +
parseInt(upper_limit / 10, 10);
console.log("lower_limit: " + lower_limit);
let digit1 = 0;
let digit2 = 0;
// Initialize result
let max_product = 0;
for (let i = upper_limit; i >= lower_limit; i--)
{
for (let j = i; j >= lower_limit; j--)
{
// calculating product of two
// n-digit numbers
let product = 999 * i * j;
if (product < max_product)
break;
let number = product;
let reverse = 0;
// calculating reverse of product
// to check whether it is
// palindrome or not
while (number != 0)
{
reverse = reverse * 10 + number % 10;
number = parseInt(number / 10, 10);
}
// update new product if exist and if
// greater than previous one
if (product == reverse && product > max_product) {
digit1 = i;
digit2 = j;
max_product = product;
}
}
}
console.log("digit1: " + digit1 + " digit2: " + digit2);
return max_product;
}
let n = 3;
document.write(larrgestPalindrome(n));
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.