- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
40 lines (34 loc) · 965 Bytes
/
Copy pathMain.java
File metadata and controls
40 lines (34 loc) · 965 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
/*
@author:mc-es
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.
Answer: 906609
*/
publicclassMain {
publicstaticvoidmain(String[] args) {
System.out.println(largestPalindrome());
}
publicstaticbooleanisPalindrome(intn) {
Stringstr = Integer.toString(n);
intlen = str.length();
for (inti = 0; i < len / 2; i++) {
if (str.charAt(i) != str.charAt(len - i - 1)) {
returnfalse;
}
}
returntrue;
}
publicstaticintlargestPalindrome() {
intlargestPalindrome = 0;
for (inti = 999; i >= 100; i--) {
for (intj = 999; j >= 100; j--) {
intproduct = i * j;
if (isPalindrome(product) && product > largestPalindrome) {
largestPalindrome = product;
}
}
}
returnlargestPalindrome;
}
}