Project Euler Problem 4 Solution – Largest Palindrome Product

Largest Palindrome Product is a pretty cool problem (problem 4) that requires you to find the largest palindrome made of a product of two 3-digit numbers. a palindrome is a number that if inverted, it has the same value (ex. 9009, 1221, 69496, etc.). Unlike other programming websites, Project Euler doesn’t care too much about performance of your code, elegance, etc. but rather wants a final answer. Since I recently stumbled on some old code I wrote last year, I figured I’d share it… the solution to problem 4 is below and written in C++. it might take a moment or two to spit the final answer, but after all Project Euler is only interested in final answer.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){
    string tempreverse;
    string temp;
    stringstream out;
    int tempnumber, tempnumber2; 
    int palindrome = 0;

    for(int i = 100; i < 1000; i++){
        for(int j = 100; j < 1000; j++){
            tempnumber = i * j;
            out << tempnumber;
            temp = out.str();
            tempreverse = string (temp.rbegin(), temp.rend());
            tempnumber2 = atoi(tempreverse.c_str());
            if (tempnumber == tempnumber2){
                if(tempnumber > palindrome){
                    palindrome = tempnumber;
    std::cout << palindrome << "n";
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
    return 0;


Leave a Reply

Your email address will not be published. Required fields are marked *