Append X digits to the end of N to make it divisible by M | Javascript program to implement the above approach ; Function to check if the value of N by appending X digits on right side of N is divisible by M or not ; Base Case ; If N is divisible by M ; Update res ; Iterate over the range [ 0 , 9 ] ; If N is divisible by M by appending X digits ; Driver Code ; Stores the number by appending X digits on the right side of N
var res = - 1 ; function isDiv ( N , X , M ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return true ; } return false ; } for ( var i = 0 ; i <= 9 ; i ++ ) { if ( isDiv ( N * 10 + i , X - 1 , M ) ) { return true ; } } } var N = 4 , M = 50 , X = 2 ; isDiv ( N , X , M , res ) ; document . write ( res ) ;