project euler 4

can anyone else tell me if this is right for project euler problem 4? i have to find the largest palindrome that is made by the sum of two three digit numbers. this is my code
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cstring>
#include <sstream>
#include <string>
using std::stringstream;
using std::cout;
using std::endl;
using std::string;

bool palindrome(string number)			// test if the number is a palindrome
{
	int num = 0;
	for (int i = 0;	i < number.length(); i++)			// convert the number to an int
	{
		num *= 10;
		num += number[i] - '0';
	}

	int num2 = 0;
	for (int i = number.length() - 1; i >= 0; i--)		// convert the number to an int but reverse the order
	{
		num2 *= 10;
		num2 += number[i] - '0';
	}
	
	return (num == num2) ? true : false;				// if the two are equal they are palindromes
}

int main(void)
{
	string num;														 									
	int pcounter = 1;								// number of palindromes found
    int palindromes[100000];

	for (int i = 100; i < 999; i++)					// get the first 10 palindromes
	{
		for (int y = 100; y < 999; y++)
		{
			stringstream ss;
			ss << i * y;
			if (palindrome(ss.str()))
			{
				palindromes[pcounter++] = i * y;
			}
		}
	}

	int largest = 0;

	for (int i = 0; i < 100000; i++)
	{
		if (palindromes[i] > largest)
			largest = palindromes[i];
	}

	cout << largest << endl;

	cout << endl;
	return 0;
}


sadly the website doesnt work for me so can anyone confirm my answer of EDITED OUT, NO CHEATING XD

thanks!
Last edited on
It's correct.
haha thanks maybe ill delete this now kinda a waste of a thread :o
It's never a good idea to delete a thread. Other people may find it useful.
:) okay
You might want to edit out the answer, though.
hehe there you go :)
Topic archived. No new replies allowed.