The program below is to find palindromes in products of 2 digit numbers (up to 10*11 for trails).
#include<iostream>
using namespace std;
int res1=0
void palindrome(int mul)
{
int k,res=0,count=0;
int p=mul;
while(mul!=0)
{
k=mul%10;
res=(res*10)+k;
mul=mul/10;
}
if(res==p)
cout<<res<<"is a palindrome:\n"
{
if(res1<res)
res1=res;
}
}
int main()
{
int mul,j;
for(int i=1;i<11;i++)
{
for(j=1;j<12;j++)
{
mul=i*j;
if(mul>10)
palindrome(mul);
}
cout<<"\n";
}
return 0;
}
The above code works for all that I need except for the largest palindrome.
My algorithm is like
- Take the product of each combination
- Use a function to check whether the result is palindrome if multiplication res is >10 and print the palindrome.
- Check for the largest number by comparing with the previous number.
So I have stored the previous value in res1 variable, and I am comparing the res and res1 variables.
According to my logic it checks lastly (88<99) that is true. Now the problem is how to print the last value stored in res variable?