Project Euler Problem4

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.


Solution:-

#include <bits/stdc++.h>
using namespace std;
#include <string>          

bool isPalin(string s)
{
    int l= s.length() - 1;
     for(int k=0; k < (s.length() / 2); k++)
     {
           if( s.at(k) != s.at(l))
             return false;
          
           l--;
     }
     return true;
}

inline void max(long int &prev_large,long int new_num)
{
    if(new_num > prev_large)
       prev_large = new_num;
   
}
int main()
{
  string s;
  int i,j;
  long int prod;
  long int large = -999;
  for(i=100; i< 1000; i++)
  {
     for(j=100; j<1000; j++)
     {
        prod = i*j;
        s = to_string(prod);
        if(isPalin(s) == true)
            max(large,prod);

     }
  }
  cout << large <<endl;
 return 0;
}

Comments

Popular posts from this blog

Project Euler Problem7

Project Euler Problem9

Project Euler Problem8