Generate all possible permutations of a Number divisible by N | C # program to implement the above approach ; 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
using System ; class GFG { static void swap_ ( char [ ] a , int l , int i ) { char temp ; temp = a [ l ] ; a [ l ] = a [ i ] ; a [ i ] = temp ; } static void permute ( char [ ] str , int l , int r , int n ) { int i ; if ( l == r ) { int j = Int32 . Parse ( new string ( str ) ) ; if ( j % n == 0 ) Console . Write ( new string ( str ) + " STRNEWLINE " ) ; return ; } for ( i = l ; i < r ; i ++ ) { swap_ ( str , l , i ) ; permute ( str , l + 1 , r , n ) ; swap_ ( str , l , i ) ; } } public static void Main ( string [ ] args ) { string str = "125" ; int n = 5 ; int len = str . Length ; if ( len > 0 ) permute ( str . ToCharArray ( ) , 0 , len , n ) ; } }