A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
#include <iostream>
#include <limits>
#include <vector>
using namespace std;
void pause() { cin.ignore(numeric_limits<streamsize>::max(), '\n'); }
bool checkIfPalindrome(long int number)
{
int digit;
vector<int> digits;
while (number > 0)
{
digit = number % 10;
number = number / 10;
digits.push_back(digit);
}
vector<int> digitsReversed;
for (int i = digits.size() -1; i >= 0; i--)
{
digitsReversed.push_back(digits[i]);
}
if (equal(digits.begin(), digits.begin() + digits.size(), digitsReversed.begin()))
{
return true;
digits.clear();
digitsReversed.clear();
}
else
{
return false;
digits.clear();
digitsReversed.clear();
}
}
int main()
{
vector<int> palindromes;
int combination = 100;
while (combination <= 999)
{
for (int i = 100; i <= 999; i++)
{
long int temp = i * combination;
if (checkIfPalindrome(temp) == true)
{
palindromes.push_back(i*combination);
}
}
combination++;
}
int max = palindromes[0];
for (int i = 1; i < palindromes.size(); i++)
{
if (palindromes[i] > max)
{
max = palindromes[i];
}
}
cout << "The max palindrome is " << max;
pause();
}