Palindrome Numbers

A palindromic number reads the same both ways eg. 10201, 12321, 14641, 40804, 44944, 69696
, here is a Java code that displays the largest Palindrome numbers from the product of two 3 digits numbers.

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
public static void main(String[] args) {
        int palin = 0;
        ArrayList<Integer> allPalin = new ArrayList<Integer>();
        String strPalin1 = "", strPalin2 = "";
        for(int i = 100; i <= 999; i++)
        {
            palin = i * i ;
            strPalin1 = Integer.toString(palin);
            strPalin2 = new StringBuffer(strPalin1).reverse().toString();
            if(strPalin1.equals(strPalin2))//finding palindromes
            {
                int aPalin = Integer.parseInt(strPalin1);//converting back to int to find biggest
                allPalin.add(aPalin);
            }
        }
        int largestYet = allPalin.get(0);//finding the largest palindrome in array
        for(int i = 0; i < allPalin.size(); i++)
        {
            int a = allPalin.get(i);
            if( a > largestYet )
                largestYet = a;
        }
        System.out.println(largestYet+" is the largest Palindrome!");
    }
}
Share This!

Leave a Reply

Your email address will not be published.