-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectEuler004.cpp
More file actions
51 lines (42 loc) · 1.12 KB
/
Copy pathprojectEuler004.cpp
File metadata and controls
51 lines (42 loc) · 1.12 KB
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
/* Problem 4
Largest Palindrome Product
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>
#include <string>
using namespace std;
bool isPalindrome (int num) {
bool isProductPalindrome = true;
string productString = to_string(num);
for (int i = 0; i < productString.length (); i++) {
if (productString [i] != productString [productString.length () - 1 - i]) {
isProductPalindrome = false;
break;
}
}
return isProductPalindrome;
}
void main () {
bool palindrome = false;
int product = 999 * 999;
int largestCandidate = 0;
for (int n1 = 999; n1 > 99; n1--) {
for (int n2 = 999; n2 > 99; n2--) {
product = n1 * n2;
palindrome = isPalindrome (product);
if (palindrome) {
if (product > largestCandidate) {
cout << n1 << " and " << n2 << endl;
largestCandidate = product;
break;
} else {
break;
}
}
}
}
cout << largestCandidate << endl;
}