-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem-4.java
52 lines (46 loc) · 1.44 KB
/
problem-4.java
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
48
49
50
51
52
public class Problem4 {
public static void main(String[] args) {
int a = longestpal();
System.out.println("The longest palindrome of 3 digit products is: " + a);
}
public static int longestpal() {
int num = 0;
String strnum = new String();
int strlen = 0;
int begind = 0;
int endind = 0;
int currpal = 0;
int frontdigit = 0;
int backdigit = 0;
for (int i = 100; i < 1000; i++) {
for (int j = 100; j < 1000; j++) {
num = i*j;
strnum = "" + num;
strlen = strnum.length();
begind = 0;
endind = strlen-1;
for (int k = 0; k < strlen; k++) {
if (begind == endind) {
if (num > currpal) {
currpal = num;
break;
}
}
frontdigit = Integer.parseInt("" + strnum.charAt(begind));
backdigit = Integer.parseInt("" + strnum.charAt(endind));
if (frontdigit == backdigit) {
if (begind + 1 == endind) {
if (num > currpal) {
currpal = num;
break;
}
}
begind++;
endind--;
}
}
}
}
return currpal;
}
}