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,870 questions

51,793 answers

573 users

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

3 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 = 100; num1 < 1000; num1++) { // 100 to 999
        int product = 99 * num1;
        for (int num2 = 100; num2 < 1000; num2++) { // 100 to 999
            product += num1;
            if (product > largestPalindrome && isPalindrome(product)) {
                largestPalindrome = product;
            }
        }
    }
      
    return largestPalindrome;
}
  
int main() {
    std::cout << getLargestPalindromeOfTwo2digitNumbers();
}
  
  
      
      
/*
run:
      
906609
      
*/
  

 



answered Oct 16, 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 = 100; num1 < 1000; num1++) { // 100 to 999
        int product = 99 * num1;
        for (int num2 = 100; num2 < 1000; num2++) { // 100 to 999
            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:
      
913 x 993 = 906609
      
*/

 



answered Oct 16, 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 = 999; num1 > 99; num1--) { // 999 to 100
        int product = 0;
        for (int num2 = num1; num2 > 99; num2--) { // 999 to 100
            product = num1 * num2;
            if (product > largestPalindrome && isPalindrome(product)) {
                largestPalindrome = product;
            }
        }
    }
    
    return largestPalindrome;
}
    
int main() {
    std::cout << getLargestPalindromeOfTwo2digitNumbers();
}
    
    
        
        
/*
run:
        
906609
    
*/

 



answered Oct 16, 2023 by avibootz
...