DEV Community

loading...

Discussion on: Project Euler #4 - Largest Palindrome Product

Collapse
rahyhub profile image
rahy-hub

Here's my code C++

/*Largest palindrome product

Problem 4
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

using namespace std;
int rev(int n);
int main()
{
int mult,largest ,p1=0,p2=0;
for(int i=100;i<=999;i++)
{
for(int j=100;j<=999;j++)
{
mult=i*j;

    if(rev(mult)==mult)
    {
        largest=mult;

            p1=i;
            p2=j;
            if(i>p1)
                p1=i;
            if(j>p2)
                p2=j;
    }
}

}
cout<<p1<<"*"<<p2<<" = "<<largest;

return 0;
}

//function to reverse the multiple

int rev(int n)
{
int r=0;
while(n!=0)
{
r=(r*10)+n%10;
n/=10;
}
return r; //we reverse the multiple without any changed in variable mult
}

C++>>Output >> 995*583 = 580085