Why does this not find the palindrome I'm looking for?

Hi,

So I have been trying to find the the largest palindrome possible from the product of 2 three digit numbers.

I have tried the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>

int check(int result)
{
	char b[100];
	sprintf(b, "%d", result);
	if (b[0] == b[5] && b[1] == b[4] && b[2] == b[3])
	{
		return 1;
	}
	else 
	{
		return 0;
	}
}

int main () {
	int i;
	int g;
	int final;
	int result;
	for (i = 999; i > 99; i--)
	{
		for (g = 999; g > 99; g--)
		{
			result = g*i;
			if (check(result) == 1)
			{
				final = result;
				goto here;
			}
		}
	}
	here:
		printf("%d", final);
}


It gives me 580085 but when I check to see if I got the answer right on Project Euler it says I got it wrong. And I don't see what I am doing wrong. Any suggestions?
The goto here is not necessary and should be avoided. (In general avoid goto, there are times when it is useful though).

put the printf statement inside the if, and on the next line have return
Also since check only returns a 1, you don't need check(result)==1 check(result works just fine.

1
2
3
4
5
6
if (check(result))
{
        printf("%d", final)
	return 0;
}


as for your question, I can't see anything wrong with your code.

You could print out all multiples of 2 3-digit numbers 10 or so at a time, and manually check.
Topic archived. No new replies allowed.