-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem4.java
More file actions
37 lines (30 loc) · 965 Bytes
/
Problem4.java
File metadata and controls
37 lines (30 loc) · 965 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
public class Problem4 {
// 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 Problem4() {
}
public static boolean isPalindrome(int number) {
int palindrome = number;
int reverse = 0;
while (palindrome != 0) {
int remainder = palindrome % 10;
reverse = reverse * 10 + remainder;
palindrome = palindrome / 10;
}
// if original and reverse of number is equal means
// number is palindrome in Java
if (number == reverse) {
return true;
}
return false;
}
public static void main(String[] args) {
for (int i = 999; i >= 900; i--) { // Upper and lower bounds
for (int j = 999; j >= 900; j--) { // It's assumed there is a palindrome in these bounds
if (isPalindrome(i * j)) {
System.out.println(i + " * " + j + " = " + i * j);
}
}
}
}
}