Largest palindrome not exceeding N which can be expressed as product of two 3 | Java program for the above approach ; Function to find the largest palindrome not exceeding N which can be expressed as the product of two 3 - digit numbers ; Stores all palindromes ; Stores the product ; Check if X is palindrome ; Check n is less than N ; If true , append it in the list ; Print the largest palindrome ; Driver Code ; Function Call
import java . util . * ; class GFG { static void palindrome_prod ( String N ) { Vector < Integer > palindrome_list = new Vector < Integer > ( ) ; for ( int i = 101 ; i < 1000 ; i ++ ) { for ( int j = 121 ; j < 1000 ; j += ( i % 11 == 0 ) ? 1 : 11 ) { int n = i * j ; String x = String . valueOf ( n ) ; String y = x ; reverse ( y ) ; if ( x == y ) { if ( n < Integer . valueOf ( N ) ) { palindrome_list . add ( i * j ) ; } } } } System . out . print ( Collections . max ( palindrome_list ) ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String N = "101110" ; palindrome_prod ( N ) ; } }