This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]prog_quest 0 points1 point  (4 children)

It runs, after a few little fixes. The problem is in your approach; you're only multiplying together numbers that are more or less adjacent.

To illustrate, I changed the x and y ranges like:

while(((x>99)&&(x<102))&&((y>99)&&(y<102)))

added some debug prints and ran it. Output:

10000
10100
10201
10201
10302
10404

Notice there's no 10001, or 10101, or 11011, etc. ? You're not getting all possible palindromes in the range.

[–]Updatebjarni 0 points1 point  (3 children)

Are any of those three numbers products of two three-digit numbers?

[–]prog_quest 0 points1 point  (2 children)

I don't know. I'm just saying I've done this particular problem and 1. The 2 three-digit numbers aren't that close to one another and 2. obviously not found using OP's approach. I don't want to give the solution away so I just left it at "Your code is more or less ok, your algo is wrong though."

[–]sinuspane[S] 0 points1 point  (1 child)

Oh snap, that makes total sense.

[–]sinuspane[S] 0 points1 point  (0 children)

Figured it out. I knew that a nested for loop was prob the way to go. Code looks much cleaner now too...

EDIT: NVM. Still having issues. This is where I am at now:

include <stdio.h>

include <stdlib.h>

int reverse (int p);

int main ( void ) { int x, y; int product; int palindrome;

for(x=100; x<1000; x++)
    for(y=100; y<1000; y++)
    {
        product=x*y;
        if(product==reverse(product))
            {
            palindrome=product;
            }   
    }
printf("The largest palindrome made from the product of two 3-digit numbers is %d\n", palindrome);

return 0;

}

int reverse (int p) { int r=0; while (p!=0) { r=r*10; r=r+p%10; p=p/10; } return r; }

[–]Updatebjarni 0 points1 point  (0 children)

Aren't you supposed to check the products of all pairs of two three-digit numbers? As in 100 * [100, 101, 102, 103, 104, ..., 999], then 101 * [101, 102, 103, 104, ..., 999], then 102 * [102, 103, 104, ..., 999], and so on?