Largest palindrome not exceeding N which can be expressed as product of two 3 | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void palindrome_prod ( string N ) { vector < int > palindrome_list ; 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 = to_string ( n ) ; string y = x ; reverse ( y . begin ( ) , y . end ( ) ) ; if ( x == y ) { if ( n < stoi ( N ) ) { palindrome_list . push_back ( i * j ) ; } } } } cout << ( * max_element ( palindrome_list . begin ( ) , palindrome_list . end ( ) ) ) ; } int main ( ) { string N = "101110" ; palindrome_prod ( N ) ; return 0 ; }