Modulus of two Hexadecimal Numbers | C ++ program to implement the above approach ; Function to calculate modulus of two Hexadecimal numbers ; Store all possible hexadecimal digits ; Iterate over the range [ '0' , '9' ] ; Convert given string to long ; Base to get 16 power ; Store N % K ; Iterate over the digits of N ; Stores i - th digit of N ; Update ans ; Update base ; Print the answer converting into hexadecimal ; Driver Code ; Given string N and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void hexaModK ( string s , string k ) { map < char , int > mp ; for ( char i = 1 ; i <= 9 ; i ++ ) { mp [ i + '0' ] = i ; } mp [ ' A ' ] = 10 ; mp [ ' B ' ] = 11 ; mp [ ' C ' ] = 12 ; mp [ ' D ' ] = 13 ; mp [ ' E ' ] = 14 ; mp [ ' F ' ] = 15 ; long m = stoi ( k , 0 , 16 ) ; long base = 1 ; long ans = 0 ; for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { long n = mp [ s [ i ] ] % m ; ans = ( ans + ( base % m * n % m ) % m ) % m ; base = ( base % m * 16 % m ) % m ; } stringstream ss ; ss << hex << ans ; string su = ss . str ( ) ; transform ( su . begin ( ) , su . end ( ) , su . begin ( ) , :: toupper ) ; cout << ( su ) ; } int main ( ) { string n = "3E8" ; string k = "13" ; hexaModK ( n , k ) ; return 0 ; }