Monday, July 16, 2012

answer for eular project-problem4


PROBLEM


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.

ANSWER
906609

JAVA CODE



public class Palindrome {
public static void main(String[]args){

int palindrome=0;
for (int i = 999; i >0; i--) {
for (int j = 999; j>0; j--) {
int prod=i*j;
String forward=Integer.toString(prod);
String reverse=new StringBuffer(forward).reverse().toString();
//System.out.println(forward+"back="+reverse);
if(forward.equalsIgnoreCase(reverse)&&prod>palindrome){

palindrome=i*j;

break;
}
}

}
System.out.println(palindrome);
}
}


0 comments:

Post a Comment