Check the divisibility of Hexadecimal numbers | C ++ implementation of the approach ; Function that returns true if s is divisible by m ; Map to map characters to real values ; To store the remainder at any stage ; Find the remainder ; Check the value of remainder ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const string CHARS = "0123456789ABCDEF " ; const int DIGITS = 16 ; bool isDivisible ( string s , int m ) { unordered_map < char , int > mp ; for ( int i = 0 ; i < DIGITS ; i ++ ) { mp [ CHARS [ i ] ] = i ; } int r = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { r = ( r * 16 + mp [ s [ i ] ] ) % m ; } if ( ! r ) return true ; return false ; } int main ( ) { string s = "10" ; int m = 3 ; if ( isDivisible ( s , m ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }