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

3 Answers

0 votes
#include <stdio.h>

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; 

    printf("%d x %d = %d", LargestNum1, LargestNum2, getLargestPalindromeOfTwo2digitNumbers(&LargestNum1, &LargestNum2));
    
    return 0;
}


    
    
/*
run:
    
91 x 99 = 9009
    
*/

 



answered Oct 15, 2023 by avibootz
edited Oct 15, 2023 by avibootz
0 votes
#include <stdio.h>

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() {
    printf("%d", getLargestPalindromeOfTwo2digitNumbers());
    
    return 0;
}


    
    
/*
run:
    
9009
    
*/

 



answered Oct 15, 2023 by avibootz
0 votes
#include <stdio.h>
  
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() {
    printf("%d", getLargestPalindromeOfTwo2digitNumbers());
      
    return 0;
}
  
  
      
      
/*
run:
      
9009
  
*/

 



answered Oct 15, 2023 by avibootz
edited Oct 16, 2023 by avibootz
...