-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLargestPalindrome.java
More file actions
34 lines (30 loc) · 1012 Bytes
/
LargestPalindrome.java
File metadata and controls
34 lines (30 loc) · 1012 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
package problem04;
/*
Largest palindrome product
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.
*/
public class LargestPalindrome {
public static void main(String[] args) {
int largestPalindrome = 0, value = 0;
String palindrome = "";
for (int one = 1; one <= 999; one++) {
for (int two = 1; two <= 999; two++) {
value = one * two;
palindrome = "" + value;
if (palindromeTest(palindrome) && value > largestPalindrome) {
largestPalindrome = value;
}
}
}
System.out.println(largestPalindrome);
}
public static boolean palindromeTest(String s) {
String s2 = "";
for (int i = s.length() - 1; i >= 0; i--) {
s2 += s.charAt(i);
}
return (s2.equals(s));
}
}