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
#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