text: Check if decimal representation of Binary String is divisible by 9 or not | C # program to implement the above approach ; Function to convert the binary string into octal representation ; Stores binary representation of the decimal value [ 0 - 7 ] ; Stores the decimal values of binary Strings [ 0 - 7 ] ; Stores length of S ; Update S ; Update S ; Update N ; Stores octal representation of the binary String ; Traverse the binary String ; Stores 3 consecutive characters of the binary String ; Append octal representation of temp ; Function to check if binary String is divisible by 9 or not ; Stores octal representation of S ; Stores sum of elements present at odd positions of oct ; Stores sum of elements present at odd positions of oct ; Stores length of oct ; Traverse the String oct ; Update oddSum ; Traverse the String oct ; Update evenSum ; Stores octal representation of 9 ; If absolute value of ( oddSum - evenSum ) is divisible by Oct_9 ; Driver Code
code: using System ; using System . Collections . Generic ; class GFG { static String ConvertequivalentBase8 ( String S ) { Dictionary < String , char > mp = new Dictionary < String , char > ( ) ; mp . Add ( "000" , '0' ) ; mp . Add ( "001" , '1' ) ; mp . Add ( "010" , '2' ) ; mp . Add ( "011" , '3' ) ; mp . Add ( "100" , '4' ) ; mp . Add ( "101" , '5' ) ; mp . Add ( "110" , '6' ) ; mp . Add ( "111" , '7' ) ; int N = S . Length ; if ( N % 3 == 2 ) { S = "0" + S ; } else if ( N % 3 == 1 ) { S = "00" + S ; } N = S . Length ; String oct = " " ; for ( int i = 0 ; i < N ; i += 3 ) { String temp = S . Substring ( 0 , N ) ; if ( mp . ContainsKey ( temp ) ) oct += mp [ temp ] ; } return oct ; } static String binString_div_9 ( String S , int N ) { String oct = " " ; oct = ConvertequivalentBase8 ( S ) ; int oddSum = 0 ; int evenSum = 0 ; int M = oct . Length ; for ( int i = 0 ; i < M ; i += 2 ) oddSum += ( oct [ i ] - '0' ) ; for ( int i = 1 ; i < M ; i += 2 ) { evenSum += ( oct [ i ] - '0' ) ; } int Oct_9 = 11 ; if ( Math . Abs ( oddSum - evenSum ) % Oct_9 == 0 ) { return " Yes " ; } return " No " ; } public static void Main ( String [ ] args ) { String S = "1010001" ; int N = S . Length ; Console . WriteLine ( binString_div_9 ( S , N ) ) ; } }