Generate all possible permutations of a Number divisible by N | Function to Swap two characters ; Function to generate all permutations and print the ones that are divisible by the N ; Convert String to integer ; Check for divisibility and print it ; Print all the permutations ; Swap characters ; Permute remaining characters ; Revoke the swaps ; Driver Code
function swap_ ( a , l , i ) { let temp ; temp = a [ l ] ; a [ l ] = a [ i ] ; a [ i ] = temp ; } function permute ( str , l , r , n ) { let i ; if ( l == r ) { let j = parseInt ( ( str ) . join ( " " ) ) ; if ( j % n == 0 ) document . write ( ( str ) . join ( " " ) + " " ) ; return ; } for ( i = l ; i < r ; i ++ ) { swap_ ( str , l , i ) ; permute ( str , l + 1 , r , n ) ; swap_ ( str , l , i ) ; } } let str = " " ; let n = 5 ; let len = str . length ; if ( len > 0 ) permute ( str . split ( " " ) , 0 , len , n ) ;