-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem4.java
55 lines (48 loc) · 1.36 KB
/
Problem4.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
53
54
55
package com.javamultiplex.projecteuler;
/**
*
* @author Rohit Agarwal
* @category Project Euler Problems
* @Problem 4- Largest palindrome product
*
*/
public class Problem4 {
public static void main(String[] args) {
int maxThreeDigit = 999;
int minThreeDigit = 100;
int multInteger = 0;
// lowest multiple of 2 three digit numbers
int max = 100 * 100;
for (int i = maxThreeDigit; i >= minThreeDigit; i--) {
for (int j = i; j >= minThreeDigit; j--) {
multInteger = i * j;
// Checking multiplication is palindrome or not
if (isPalindrome(multInteger)) {
if (multInteger > max) {
max = multInteger;
break;
}
}
}
}
System.out.println("Largest palindrom made from the product of two " + "3-digit numbers is: " + max);
}
private static boolean isPalindrome(int multInteger) {
boolean result = false;
String multString = null;
StringBuffer multStringBuffer = null;
// Converting Integer to String
multString = String.valueOf(multInteger);
/*
* Converting String to StringBuffer, We want to reverse the string
* thats why we have to use StringBuffer.
*/
multStringBuffer = new StringBuffer(multString);
if (multString.equals(multStringBuffer.reverse().toString())) {
result = true;
} else {
result = false;
}
return result;
}
}