-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem004.java
More file actions
44 lines (44 loc) · 1.39 KB
/
Problem004.java
File metadata and controls
44 lines (44 loc) · 1.39 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
import java.util.ArrayList;
/**
* Finds the largest palindromic product of two three digit integers
* @author Sean Titus
* @version 1.0
*/
public class Problem4 {
/**
* Determines whether or not a String is a palindrome, not case sensitive
* @param word The String being tested as a palindrome
* @return A boolean of whether or not the input was a palindrome
*/
public static boolean isPalindrome(String word) {
word = word.toLowerCase();
char[] wordArray = word.toCharArray();
String reverse = "";
for (int i = wordArray.length - 1; i >= 0; i--) {
reverse += wordArray[i];
}
return reverse.equals(word);
}
/**
* Main class, multiplies three digit ints and finds largest palindrome
* @param args Command line arguments that aren't used
*/
public static void main(String[] args) {
ArrayList<Integer> palindromes = new ArrayList<Integer>();
for(int i = 100; i < 1000; i++) {
for(int j = 100; j < 1000; j++) {
int num = j * i;
if (isPalindrome(Integer.toString(num))) {
palindromes.add(num);
}
}
}
int largest = 0;
for (int i : palindromes) {
if (i > largest) {
largest = i;
}
}
System.out.println(largest);
}
}