text: Check if Decimal representation of given Binary String is divisible by K or not | C # program for above approach ; Function to check the binary number is divisible by K ; Array poweroftwo will store pow ( 2 , i ) % k ; Initializing the first element in Array ; Storing every pow ( 2 , i ) % k value in the array ; To store the remaining ; Iterating till N ; If current bit is 1 ; Updating rem ; If completely divisible ; If not Completely divisible ; Driver Code ; Given Input ; length of string s ; Function Call
code: using System ; class GFG { public static String divisibleByk ( String s , int n , int k ) { int [ ] poweroftwo = new int [ n ] ; poweroftwo [ 0 ] = 1 % k ; for ( int i = 1 ; i < n ; i ++ ) { poweroftwo [ i ] = ( poweroftwo [ i - 1 ] * ( 2 % k ) ) % k ; } int rem = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ n - i - 1 ] == '1' ) { rem += ( poweroftwo [ i ] ) ; rem %= k ; } } if ( rem == 0 ) { return " Yes " ; } else return " No " ; } public static void Main ( String [ ] args ) { String s = "1010001" ; int k = 9 ; int n = s . Length ; Console . Write ( divisibleByk ( s , n , k ) ) ; } }