-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler004.cpp
More file actions
49 lines (36 loc) · 969 Bytes
/
euler004.cpp
File metadata and controls
49 lines (36 loc) · 969 Bytes
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
/*projecteuler.net problem 4
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers.
*/
#include <iostream>
using namespace std;
bool isNumericPalindrome(int number)
{
int index=0;
//digit cannot be greater than 9, so initialize to 10 to handle <6 digit numbers
int digits[6]={10,10,10,10,10,10};
do{
digits[index++]=number%10;
number/=10;
} while(number>0);
if(digits[5]==digits[0] && digits[4]==digits[1] && digits[3]==digits[2] )
{
return 1;
}
return 0;
}
int main(){
int largestPalindrome = 0;
for(int a=999; a>99; a--)
{
for (int b=a; b>99; b--)
{
if ((a*b > largestPalindrome) && isNumericPalindrome(a*b))
{
largestPalindrome=a*b;
}
}
}
cout << "Largest palindrome made from the product of two 3-digit numbers is: " << largestPalindrome <<endl;
return 0;
}