Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to find the largest palindrome made from the product of two 2-digit numbers in C++

4 Answers

0 votes
#include <iostream>
  
static int isPalindrome(int num) {
    int temp = num, rev = 0;
     
    while (temp > 0) {
        rev = rev * 10 + (temp % 10);
        temp /= 10;
    }
 
    return rev == num;
}
 
static int getLargestPalindromeOfTwo2digitNumbers(void) {
    int largestPalindrome = -1;
     
    for (int num1 = 10; num1 < 100; num1++) { // 10 to 99
        int product = 9 * num1;
        for (int num2 = 10; num2 < 100; num2++) { // 10 to 99
            product += num1;
            if (product > largestPalindrome && isPalindrome(product)) {
                largestPalindrome = product;
            }
        }
    }
     
    return largestPalindrome;
}
 
int main() {
    std::cout << getLargestPalindromeOfTwo2digitNumbers();
}
 
 
     
     
/*
run:
     
9009
     
*/
  

 



answered Oct 15, 2023 by avibootz
0 votes
#include <iostream>
  
static int isPalindrome(int num) {
    int temp = num, rev = 0;
     
    while (temp > 0) {
        rev = rev * 10 + (temp % 10);
        temp /= 10;
    }
 
    return rev == num;
}
 
static int getLargestPalindromeOfTwo2digitNumbers(int* LargestNum1, int* LargestNum2) {
    int largestPalindrome = -1;
     
    for (int num1 = 10; num1 < 100; num1++) { // 10 to 99
        int product = 9 * num1;
        for (int num2 = 10; num2 < 100; num2++) { // 10 to 99
            product += num1;
            if (product > largestPalindrome && isPalindrome(product)) {
                *LargestNum1 = num1;
                *LargestNum2 = num2;
                largestPalindrome = product;
            }
        }
    }
     
    return largestPalindrome;
}
 
int main() {
    int LargestNum1 = 0, LargestNum2 = 0; 
    
    int result = getLargestPalindromeOfTwo2digitNumbers(&LargestNum1, &LargestNum2);
 
    std::cout << LargestNum1 << " x " << LargestNum2 << " = " << result;

}
 
 
     
     
/*
run:
     
91 x 99 = 9009
     
*/
  

 



answered Oct 15, 2023 by avibootz
0 votes
#include <iostream>
   
static int isPalindrome(int num) {
    int temp = num, rev = 0;
       
    while (temp > 0) {
        rev = rev * 10 + (temp % 10);
        temp /= 10;
    }
   
    return rev == num;
}
  
static int getLargestPalindromeOfTwo2digitNumbers(void) {
    int largestPalindrome = -1;
   
    for (int num1 = 99; num1 > 9; num1--) { // 99 to 10
        int product = 0;
        for (int num2 = num1; num2 > 9; num2--) { // 99 to 10
            product = num1 * num2;
            if (product > largestPalindrome && isPalindrome(product)) {
                largestPalindrome = product;
            }
        }
    }
   
    return largestPalindrome;
}
   
int main() {
    std::cout << getLargestPalindromeOfTwo2digitNumbers();
}
   
   
       
       
/*
run:
       
9009
   
*/

 



answered Oct 15, 2023 by avibootz
edited Oct 16, 2023 by avibootz
0 votes
#include <iostream>
#include <string>
#include <algorithm>

// Function to check if a number is a palindrome
bool isPalindrome(int number) {
    std::string str = std::to_string(number);
    std::string reversedStr = str;
    std::reverse(reversedStr.begin(), reversedStr.end());
    
    return str == reversedStr;
}

static int getLargestPalindromeOfTwo2digitNumbers(void) {
   int largestPalindrome = 0;

    for (int i = 10; i < 100; i++) {
        for (int j = 10; j < 100; j++) {
            int product = i * j;
            if (isPalindrome(product) && product > largestPalindrome) {
                largestPalindrome = product;
            }
        }
    }   
      
    return largestPalindrome;
}
  
int main() {
    std::cout << "The largest palindrome made from the product of two 2-digit numbers is: " <<  getLargestPalindromeOfTwo2digitNumbers() << "\n";
}

 
 
/*
run:
 
The largest palindrome made from the product of two 2-digit numbers is: 9009
 
*/
   

 



answered May 11, 2025 by avibootz
...