I was trying to solve a problem of Project Euler with Java. The Problem 4.
http://projecteuler.net/problem=4
It said I had to find the largest palindrome made from the product of two 3-digit numbers.
So here is the code (just the part that isn't working) :
public class Problem4PE {
public static void main(String[] args){
int result = 0;
Integer output = 0;
for (int i = 100; i < 1000; i++) {
for (int j = 100; j < 1000; j++) {
output = i*j;
String strout = output.toString();
StringBuilder stb = new StringBuilder();
char[] chrout = strout.toCharArray();
for (int x = 0; x < chrout.length; x++) {
stb.append(chrout[x]);
}
StringBuilder stres = new StringBuilder("");
for (int k = chrout.length-1; k > -1 ; k--) {
stres.append(chrout[k]);
}
String stbb = stb.toString();
String string = stres.toString();
Boolean bool = stbb.equals(string);
if (bool){
result = output;
}
}
}
System.out.println(result);
}
}
So aboves output comes out : 580085 But when I submit it, it shows incorrect. I do not understand why. So could you please tell me whether the problem is in code, or I had a mistake in understanding the question.
Try this
if (bool && output > result) { // a second if condition.
result = output;
}
It gives me the correct answer.