Computer Science, asked by Krey16, 9 months ago

Write a program in Java to display the largest four-digit even palindromic number.

Answers

Answered by rushilansari
0

Answer:

// Java problem to find out the  

// largest palindrome number  

// which is product of two  

// n digit numbers.  

 

class GFG  

{  

   // Function to calculate largest  

   // palindrome which isproduct of  

   // two n-digits numbers  

   static int larrgestPalindrome(int n)  

   {  

       int upper_limit = 0;  

     

       // Loop to calculate upper bound  

       // (largest number    of n-digit)  

       for (int i=1; i<=n; i++)  

       {  

           upper_limit *= 10;  

           upper_limit += 9;  

       }  

     

       // largest number of n-1 digit.  

       // One plus this number  

       // is lower limit which is  

       // product of two numbers.  

       int lower_limit = 1 + upper_limit / 10;  

     

       // Initialize result  

       int max_product = 0;  

         

       for (int i = upper_limit; i >= lower_limit; i--)  

       {  

           for (int j = i; j >= lower_limit; j--)  

           {  

               // calculating product of two  

               // n-digit numbers  

               int product = i * j;  

               if (product < max_product)  

                   break;  

               int number = product;  

               int reverse = 0;  

     

               // calculating reverse of product  

               // to check whether it is  

               // palindrome or not  

               while (number != 0)  

               {  

                   reverse = reverse * 10 + number % 10;  

                   number /= 10;  

               }  

     

               // update new product if exist and if  

               // greater than previous one  

               if (product == reverse && product > max_product)  

                   max_product = product;  

           }  

       }  

       return max_product;  

   }  

     

   // Driver code  

   public static void main (String[] args)  

   {  

     

       int n = 2;  

       System.out.print(larrgestPalindrome(n));  

   }  

}

Similar questions