Thursday, January 19, 2012

largest palindrome made by mul of three digits

/*
 * bhatt.umang7@gmail.com
 */

/*
 * 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 class Problem4

{
    static long reverse(long no)
    {
        long reverse = 0 ;

        while (no  > 0 )
        {
            reverse *=10;

            reverse += no% 10;
            no/=10;
        }

        return reverse;    
    }
    public static boolean isPalindrome(long no)
    {
        long reverse = reverse(no);

        boolean returnVal = false ; 
        if (reverse == no )
        {

            returnVal = true;
        }
        return returnVal;
    }
    public static void main(String args[])
    {

        
        int n1 = 1000;
        int n2 = 1000;

        long ans =0 ;
        for (int i = 0 ; i < n1 ; i++ )
        {

            for (int j = 0 ; j < n1 ; j++ )
            {

                long temp =  i * j ;
                if (isPalindrome(temp))
                {

                    if (ans < temp)
                    {
                        ans = temp ;
                    }
                }
            }
        }

    
        System.out.println("Ans is "+ ans );
    }
}

No comments:

Post a Comment