forked from devForTheFuture/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestPalindromeProduct.java
More file actions
37 lines (28 loc) · 849 Bytes
/
LargestPalindromeProduct.java
File metadata and controls
37 lines (28 loc) · 849 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
package algorithms;
/**
* 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.
*
* @author joeytawadrous
*/
public class LargestPalindromeProduct {
public static void main(String[] args) {
System.out.println(sixDigitPalindrome());
}
public static int sixDigitPalindrome() {
int max = 0;
for(int i = 999; i > 100; i--) {
for(int j = i; j > 100; j--) {
String number = Integer.toString((i * j));
if(number.length() == 6
&& number.charAt(0) == number.charAt(5)
&& number.charAt(1) == number.charAt(4)
&& number.charAt(2) == number.charAt(3)
&& Integer.parseInt(number) > max) {
max = i * j;
}
}
}
return max;
}
}