PROBLEM :
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.
I came up with a solution for the above problem. I used a linked list to save the digits, compare them to find palindromes. If found as palindromes, the are pushed into a vector and the whole vector is displayed in the last.
PS : As the list functions will occupy lots of space, im not posting the list functions here.
#include <iostream>
#include <vector>
using namespace std;
vector<int> palindromes;
vector<int>::iterator it;
struct llist
{
int value;
llist *next;
};
llist *start = NULL;
void insert(int);
void delete_list();
void disp();
bool is_pal();
bool is_pal() /* List Function to check for palindromes*/
{
int d[6], i=0;//, count = 0;
llist *temp;
temp = start;
while (temp->next != NULL)
{
d[i] = temp->value;
i++;
//count++;
temp = temp->next;
}
d[i] = temp->value;
//count++;
//cout << "No of digits in the no : " << count << endl;
if ( (d[0]==d[5]) && (d[1]==d[4]) && (d[2]==d[3]) )
return true;
delete_list();
}
class palindrome
{
public:
int stand;
void product();
void is_palindrome(int);
};
void palindrome:: product()
{
stand = 999;
int temp = 999;
int prod = 1;
while (stand>100)
{
while (temp>100)
{
prod = stand * temp;
//cout << "No Tested : " << prod << endl;
is_palindrome(prod);
temp--;
}
stand--;
}
}
void palindrome:: is_palindrome(int x)
{
int r, t;
t = x;
bool flag;
while (t>=10)
{
r = t%10;
insert(r);
t /= 10;
}
insert(t);
//cout << "Digits in the number : ";
//disp();
cout << "\n\n";
flag = is_pal();
if (flag==true)
{
palindromes.push_back(x);
//cout << "\n" << x << endl;
}
}
int main()
{
palindrome inst;
inst.product();
cout << "Palindromes\n\n";
for (it = palindromes.begin(); it != palindromes.end(); it++)
cout << (*it) << endl;
cout << "\n\n";
}