-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem4.java
More file actions
47 lines (41 loc) · 983 Bytes
/
Problem4.java
File metadata and controls
47 lines (41 loc) · 983 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
41
42
43
44
45
46
47
public class Problem4 {
public Integer largest_palindrome_product() {
int largestpalindrome = 0;
int firstNum = 100;
int secondNum = 100;
int product = 0;
ArrayList<Integer> al = new ArrayList<Integer>();
while (firstNum <= 999) {
while (secondNum <= 999) {
product = firstNum * secondNum;
if (checkPalindrome(product) == true) {
al.add(product);
}
secondNum++;
}
secondNum = 100;
firstNum++;
}
Integer array[] = al.toArray(new Integer [al.size()]);
largestpalindrome = array[0];
for (int i = 0; i < array.length - 1 ;i++) {
if(array[i+1] > largestpalindrome) {
largestpalindrome = array[i+1];
}
}
return largestpalindrome;
}
boolean checkPalindrome(int num) {
int reversedNum = 0;
int input = num;
while (input != 0) {
reversedNum = reversedNum * 10 + input % 10;
input = input / 10;
}
if (reversedNum == num) {
return true;
} else {
return false;
}
}
}