简体   繁体   中英

Largest palindrome made from the product of two 3-digit numbers with C

The code is trying to find the largest palindrome made from the product of two 2-digit numbers. The answer is 91*99 = 9009 but I keep getting 990, which is not even a palindrome. I really appreciate the help!

#include <stdio.h>

int main()

{
    int i = 10;
    int j = 10;
    int a = 0;
    int b = 0;
    int array[100] = {0};
    int divider = 10;
    int num;
    int great;
    int product;
    int n;
    int flag;

    /*Loop through first 2 digit number and second 2 digit number*/

    while (i<100)
    {
        while (j < 100)
        {
            product = i*j;
            array [a] = product % 10;
            n = product / divider; 

            while (n != 0)
            {
                a++; 
                num = n%10;
                divider *=10;
                array[a]=num;
                n = product/divider;
            }

            flag = 0;

            while (b<a) 
            {
                if (array[b] != array[a])
                {
                    flag = 1;   
                }
                b++;
                a--;
            }

            if (flag == 0)
            {
                great = product;
            }

            j++;
            a = 0;
            b = 0;
        }     
        i++;
    }

    printf("The largest palindrome is %d \n", great);

    return 0;
}

Here is a code snippet you can try.

    #include <stdio.h>

void main()
{
int a = 1;      // first integer
int b = 1;     // second integer
int currentNumber;      
int currentPalin;      if a palindrome is found, its stored here

while (a<100){      //loop through the first number

        while (b<100){      // loop through the second number
            currentNumber = a*b;
            if (currentNumber == reverse(currentNumber) ){      //check for palindrome
                currentPalin = currentNumber;

            }
            b = b+1;      //increment the second number

        }
        b = a; // you could have set b=1 but it would not be an efficient algorithm because
            //some of the multiplication would occur twice. eg- (54*60) and (60*54)
        a = a +1;      //increment the first number
    }
printf ("Largest palindrom is %d  \n", currentPalin);

getchar();

}
// method for finding out reverse
int reverse(int n){
    int reverse = 0;



 while (n != 0)
{
    reverse = reverse * 10;
    reverse = reverse + n%10; 

// when you divide a number by 10, the 
//remainder gives you the last digit. so you are reconstructing the 
//digit from the last

    n = n/10;
}

return reverse;


}

Update:- As suggested by M Oehm, I have modified the code to make it more general.

#include <stdio.h>

void main()
{
int a = 1;
int b = 1;
int currentNumber;
int currentPalin=0;

while (a<100){

        while (b<100){
            currentNumber = a*b;
            if (currentNumber == reverse(currentNumber) ){
                if (currentNumber>currentPalin){
                        currentPalin = currentNumber;                       
                    }

            }
            b = b+1;

        }
        b = 1; 
        a = a +1;
    }
if (currentPalin==0){
    printf("No Palindrome exits in this range");
}
else {
    printf ("Largest palindrome is %d  \n", currentPalin);
}

getchar();

}

int reverse(int n){
    int reverse = 0;



 while (n != 0)
{
    reverse = reverse * 10;
    reverse = reverse + n%10;
    n = n/10;
}

return reverse;


}

An alternative approach to solve the problem.

#include<stdio.h>

int reverse(int num)
{
    int result = 0;
    while( num > 0)
    {
        result = result * 10 + (num%10);
        num/=10;
    }
    return result;
}

int main()
{
    int last_best = 1;
    int best_i=1;
    int best_j = 1;
    const int max_value = 99;

    for( int i = max_value ; i > 0 ; --i)
    {
        for(int j = i ; j > 0 ; --j){
            int a = i * j;
            if( last_best > a )
                break;
            else if ( a == reverse(a) )
            {
                last_best = a;
                best_i = i;
                best_j = j;
            }
        }
    }
    printf("%d and %d = %d\n", best_i,best_j,last_best);
}

And it is quite simple to follow.

It seems that you do not reinitialize variables at the beginning of loop. They keeps values from previous iterations. For example, j and divider . Put

  j = 10;

before starting "j" loop, ie:

   j = 10;
   while (j < 100) ...

The same for divider:

     ...
     j = 10;
     while (j < 100) {
             divider = 10;
             ...

If you were using for loops you would avoid this problem naturally:

for(i=10; i<100; i++) {
  for(j=10; j<100; j++) { 
     ...
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM